1 / 33

Graohics

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).

myra
Download Presentation

Graohics

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Graohics CSC 171 FALL 2001 LECTURE 16

  2. 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

  3. Origin in upper left X increases to right Y increases down Coordinate System Increasing x (0,0) Increasing y

  4. So: g.drawLine(30,40,50,70); Coordinate System Increasing x (0,0) (30,40) (50,70) Increasing y

  5. Basic Graphics Operations • Translation • Change the position • Scale • Change the size • Rotation • Change the angle

  6. /*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

  7. /*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)

  8. 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)

  9. 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

  10. 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

  11. 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

  12. 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)

  13. 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)

  14. 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)

  15. 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)

  16. x2 = = x1*cosf-y1*sinf y2 = = x1*sina+y1*cosf Rotation about the origin a f (x1,y1) (x2,y2)

  17. x2 = x1*cosf-y1*sinf y2 = x1*sina+y1*cosf Rotation as a Matrix operation

  18. 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)

  19. 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)

  20. Rotation about the Origin • Not “In Place” • How do we do “in place”?

  21. 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

  22. FILE IO • FileReader class • read() method gets a character • FileWriter class • write() method writes a character

  23. The Caesar Cipher

  24. 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

  25. 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]);

  26. } } } 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(); }

  27. 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); }

  28. /** 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; }

  29. /** 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; }

  30. Whole class

  31. Passing class

  32. All exams & projects in 73/134

  33. All exam & projects in 73/134

More Related