330 likes | 506 Views
Graohics. CSC 171 FALL 2001 LECTURE 16. 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).
E N D
Graohics CSC 171 FALL 2001 LECTURE 16
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. History: COBOL
Origin in upper left X increases to right Y increases down Coordinate System Increasing x (0,0) Increasing y
So: g.drawLine(30,40,50,70); Coordinate System Increasing x (0,0) (30,40) (50,70) Increasing y
Basic Graphics Operations • Translation • Change the position • Scale • Change the size • Rotation • Change the angle
/*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); Translation Increasing x (0,0) (30,40) (50,50) (80,90) (60,80) Increasing y
/*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” Scale Increasing x (0,0) (30,40) (60,80) (60,80) Increasing y (120,160)
Given “scale” and “translation” as above: How would you do “translation in place”? (ie : keep center of line in place, alter size) Question (15,20) (30,40) (60,80) (75,100)
int x1,y1,x2,y2; x1=30; y1=40; x2=60; y2=80; g.drawLine(x1,y1,x2,y2); //center point int averageX = (x1+x2)/2; Int averageY = (y1+y2)/2; //put center @ origin x1-=averageX; x2-=averageX; y1-=averageY; y2-=averageY; //scale as before x1*=2; x2*=2; y1*= 2; y2*=2; //center is still at center //so, return center to same //put center @ origin x1+=averageX; x2+=averageX; x1+=averageY; y2+=averageY; g.drawLine(x1,y1,x2,y2); Scale in place
int x1,y1,x2,y2; x1=30; y1=40; x2=60; y2=80; g.drawLine(x1,y1,x2,y2); //center point int averageX = (x1+x2)/2; //45 Int averageY = (y1+y2)/2; //60 //put center @ origin x1-=averageX; // 30-45 = = -15 x2-=averageX; // 60-45 = = 15 y1-=averageY; // 40-60 = = -20 y2-=averageY; // 80-60 = = 20 //scale as before x1*=2; // -15 * 2 = = -30 x2*=2; // 15 * 2 = = 30 y1*= 2; // -20 * 2 = = -40 y2*=2; // 20 * 2 = = 40 //center is still at center //so, return center to same //put center @ origin x1+=averageX; // -30 + 45 = =15 x2+=averageX; // 30+45 = = 75 x1+=averageY; // -40+60 = =20 y2+=averageY; // 40+60 = =100 g.drawLine(x1,y1,x2,y2); Scale in place
Rotation • So, we can translate • Scale, in place • What about rotation • Rotation involves “angles” • Angles involve math • In computer science math is used for application as well as analysis
Recall: On the unit circle, Give the angle F x=cos(F) y=sin(F) A little trigonometry (0,-1) x F (-1,0) (1,0) y (x,y) (0,1) y=sin(F) x=cos(F)
x=r*cos(F) y=r*sin(F) Rotation about the origin is going from one point on the circle to another (adding an angle) Any point in the plane (0,-r) x F (-r,0) (r,0) y (x,y) (0,r) y=r*sin(F) x=r*cos(F)
Going from (x1,y1) add f to a is like finding the sum Remember: cos(a+f) == cosa*cosf-sina*sinf sin(a+f) == sina*cosf+cosa*sinf Angle Addition a f (x1,y1) (x2,y2)
x1=r*cos(a) y1=r*sin(a) x2 = r*cos(a+f) == r* (cosa*cosf-sina*sinf) = = x1*cosf-y1*sinf y2= r* sin(a+f) == r*(sina*cosf+cosa*sinf) = = x1*sinf+y1*cosf Angle Addition a f (x1,y1) (x2,y2)
x2 = = x1*cosf-y1*sinf y2 = = x1*sina+y1*cosf Rotation about the origin a f (x1,y1) (x2,y2)
x2 = x1*cosf-y1*sinf y2 = x1*sina+y1*cosf Rotation as a Matrix operation
int 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?? Rotation by 90 degrees (0,0) (30,40) (60,80)
x1p =x1*0-y1*1 // -40 y1p = y1*0+x1*1// 30 x2p = x2*0-y2*1 //-80 y2p = y2*0+x2*1 //60 Rotation by 90 degrees (0,0) (-40,30) (30,40) (-80,60) (60,80)
Rotation about the Origin • Not “In Place” • How do we do “in place”?
Rotation about the Origin • Not “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 IO • FileReader class • read() method gets a character • FileWriter class • write() method writes a character
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; }