graohics csc 171 fall 2001 lecture 16. history: cobol 1960 - conference on data system languages...
TRANSCRIPT
GraohicsGraohics
CSC 171 FALL 2001
LECTURE 16
History: COBOLHistory: COBOL 1960 - Conference on Data
System Languages (CODASYL) - led by Joe Wegstein of NBS developed the first standardized business computer programming language, COBOL (Common Business Oriented Language).
For the next 20 years there were more programs written in COBOL than any other single language.
That same year the second of the mathematical languages, ALGOL 60 was developed, not widely implemented ALGOL became the conceptual basis of many programming languages.
Coordinate SystemCoordinate System Origin in upper left X increases to right Y increases down
(0,0) Increasing x
Increasing y
Coordinate SystemCoordinate System So:
g.drawLine(30,40,50,70);
(0,0) Increasing x
Increasing y
(30,40)
(50,70)
Basic Graphics OperationsBasic Graphics Operations
Translation– Change the position
Scale– Change the size
Rotation– Change the angle
TranslationTranslation/*Moving an object is addition */
int x1,y1,x2,y2;
x1=30; y1=40; x2=60; y2=80;
g.drawLine(x1,y1,x2,y2);
//20 to the left , 10 down
x1+=20; x2+=20;
y1+= 10; y2+=10;
g.drawLine(x1,y1,x2,y2);
(0,0) Increasing x
Increasing y
(30,40)
(60,80)
(50,50)
(80,90)
ScaleScale/*Sizing an object multiplication */
int x1,y1,x2,y2;
x1=30; y1=40; x2=60; y2=80;
g.drawLine(x1,y1,x2,y2);
//double the size
x1*=2; x2*=2;
y1*= 2; y2*=2;
g.drawLine(x1,y1,x2,y2);
// NOT “in place”
(0,0) Increasing x
Increasing y
(30,40)
(60,80)
(60,80)
(120,160)
QuestionQuestion
Given “scale” and “translation” as above:
How would you do “translation in place”?
(ie : keep center of line in place, alter size)
(30,40)
(60,80)
(15,20)
(75,100)
Scale in placeScale in place
int x1,y1,x2,y2;x1=30; y1=40; x2=60; y2=80;g.drawLine(x1,y1,x2,y2);
//center pointint averageX = (x1+x2)/2;Int averageY = (y1+y2)/2;
//put center @ originx1-=averageX; x2-=averageX;y1-=averageY; y2-=averageY;
//scale as beforex1*=2; x2*=2;y1*= 2; y2*=2;
//center is still at center//so, return center to same//put center @ originx1+=averageX; x2+=averageX;x1+=averageY; y2+=averageY;
g.drawLine(x1,y1,x2,y2);
Scale in placeScale in place
int x1,y1,x2,y2;x1=30; y1=40; x2=60; y2=80;g.drawLine(x1,y1,x2,y2);
//center pointint averageX = (x1+x2)/2; //45Int averageY = (y1+y2)/2; //60
//put center @ originx1-=averageX; // 30-45 = = -15x2-=averageX; // 60-45 = = 15y1-=averageY; // 40-60 = = -20y2-=averageY; // 80-60 = = 20
//scale as beforex1*=2; // -15 * 2 = = -30x2*=2; // 15 * 2 = = 30y1*= 2; // -20 * 2 = = -40y2*=2; // 20 * 2 = = 40
//center is still at center//so, return center to same//put center @ originx1+=averageX; // -30 + 45 = =15x2+=averageX; // 30+45 = = 75x1+=averageY; // -40+60 = =20y2+=averageY; // 40+60 = =100
g.drawLine(x1,y1,x2,y2);
Rotation Rotation So, we can translateScale, in place
What about rotation– Rotation involves “angles”– Angles involve math
In computer science math is used for application as well as analysis
A little trigonometryA little trigonometry
Recall:
On the unit circle,
Give the angle x=cos()
y=sin()(x,y)
x=cos()
y=sin()
(1,0)(-1,0)
(0,1)
(0,-1)
y
x
Any point in the planeAny point in the plane
x=r*cos()
y=r*sin()
Rotation about the origin is going from one point on the circle to another
(adding an angle) (x,y)
x=r*cos()
y=r*sin()
y
x
(0,r)
(0,-r)
(r,0)(-r,0)
Angle AdditionAngle AdditionGoing from (x1,y1)
add to is like finding the sum
Remember:
cos(+) ==
cos*cos-sin*sin
sin(+) ==
sin*cos+cos*sin
(x1,y1)
(x2,y2)
Angle AdditionAngle Additionx1=r*cos()y1=r*sin()
x2 = r*cos(+) ==r* (cos*cos-sin*sin
x1*cos-y1*sin
y2= r* sin(+) ==
r*(sin*cos+cos*sinx1*siny1*cos
(x1,y1)
(x2,y2)
Rotation about the originRotation about the originx2 x1*cos-y1*sin
y2 x1*sin+y1*cos
(x1,y1)
(x2,y2)
Rotation as a Matrix operationRotation as a Matrix operation
x2 x1*cos-y1*sin
y2 x1*sin+y1*cos
1
1
)cos()sin(
)sin()cos(
2
2
y
x
y
x
Rotation by 90 degreesRotation by 90 degreesint x1,y1,x2,y2;
x1=30; y1=40; x2=60; y2=80;
g.drawLine(x1,y1,x2,y2);
int x1p,y1p,x2p,y2p
//sin(90) == 1
//cos(90) == 0
//what happens??
(0,0)
(30,40)
(60,80)
Rotation by 90 degreesRotation by 90 degreesx1p =x1*0-y1*1 // -40
y1p = y1*0+x1*1// 30
x2p = x2*0-y2*1 //-80
y2p = y2*0+x2*1 //60
(0,0)
(30,40)
(60,80)
(-40,30)
(-80,60)
Rotation about the OriginRotation about the OriginNot “In Place”
How do we do “in place”?
Rotation about the OriginRotation about the OriginNot “In Place”
How do we do “in place”?
The same way as scale “in place”– Translate center of object to origin– Rotate around origin– Translate back
FILE IOFILE IO
FileReader class– read() method gets a character
FileWriter class – write() method writes a character
The Caesar Cipher
Program Crypt.java
import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;
public class Crypt{ public static void main(String[] args) { boolean decrypt = false; int key = DEFAULT_KEY; FileReader infile = null; FileWriter outfile = null;
if (args.length < 2 || args.length > 4) usage();
// gather command line arguments and open files
try{ for(int i = 0; i < args.length; i++) { if (args[i].substring(0, 1).equals("-")) // it is a command line option { String option = args[i].substring(1, 2); if (option.equals("d")) decrypt = true; else if (option.equals("k")) { key = Integer.parseInt (args[i].substring(2)); if (key < 1 || key >= NLETTERS) usage(); } } else { if (infile == null) infile = new FileReader(args[i]); else if (outfile == null) outfile = new FileWriter(args[i]);
} }}catch(IOException e){ System.out.println("Error opening file"); System.exit(0);}
if (infile == null || outfile == null) usage();
// encrypt or decrypt the input
if (decrypt) key = NLETTERS - key;
try{ encryptFile(infile, outfile, key); infile.close(); outfile.close();}
catch(IOException e) { System.out.println("Error processing file"); System.exit(0); }}
/** Prints a message describing proper usage and exits.*/public static void usage(){ System.out.println ("Usage: java Crypt [-d] [-kn] infile outfile"); System.exit(1);}
/** Encrypts a character with the Caesar cipher. Only upper- and lowercase letters are encrypted. @param c the character to encrypt @param k the encryption key @return the encrypted character*/public static char encrypt(char c, int k){ if ('a’ <= c && c <= 'z') return (char)('a’ + (c - 'a’ + k) % NLETTERS); if ('A’ <= c && c <= 'Z') return (char)('A’ + (c - 'A’ + k) % NLETTERS);
return c;}
/** Encrypts all characters in a file. @param in the plaintext file @param out the file to store the encrypted characters @param k the encryption key*/ public static void encryptFile(FileReader in, FileWriter out, int k) throws IOException { while (true) { int next = in.read(); if (next == -1)return; // end of file char c = (char)next; out.write(encrypt(c, k)); } }
public static final int DEFAULT_KEY = 3; public static final int NLETTERS = 'z’ - 'a’ + 1;}
CSC 171 GRADES FALL 2001
0
10
20
30
40
50
60
I N E D- D D+ C- C C+ B- B B+ A- A
GRADE
CO
UN
T
Whole classWhole class
CSC 171 GRADES FALL 2001
0
2
4
6
8
10
12
I N E D- D D+ C- C C+ B- B B+ A- A
GRADE
CO
UN
T
Passing classPassing class
All exams & projects in 73/134All exams & projects in 73/134CSC 171 GRADES FALL 2001
0
2
4
6
8
10
12
I N E D- D D+ C- C C+ B- B B+ A- A
GRADE
CO
UN
T
All exam & projects in 73/134All exam & projects in 73/134
E 0.00% 5 6.85% 100.00%D- 60.00% 3 4.11% 93.15%D 63.00% 6 8.22% 89.04%D+ 67.00% 3 4.11% 80.82%C- 70.00% 5 6.85% 76.71%C 73.00% 10 13.70% 69.86%C+ 77.00% 10 13.70% 56.16%B- 80.00% 7 9.59% 42.47%B 83.00% 7 9.59% 32.88%B+ 87.00% 6 8.22% 23.29%A- 90.00% 4 5.48% 15.07%A 93.00% 7 9.59% 9.59%