Download presentation

Presentation is loading. Please wait.

Published byJaylynn Narramore Modified over 2 years ago

1
Graohics CSC 171 FALL 2001 LECTURE 16

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

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

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

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

6
Translation /*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)

7
Scale /*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) (120,160)

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

9
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; 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);

10
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);

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
A 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

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

14
Angle Addition Going 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)

15
Angle Addition x1=r*cos( ) y1=r*sin( ) x2 = r*cos( + ) == r* (cos *cos -sin *sin x1*cos -y1*sin y2= r* sin( + ) == r*(sin *cos +cos *sin x1*sin y1*cos (x1,y1) (x2,y2)

16
Rotation about the origin x2 x1*cos -y1*sin y2 x1*sin +y1*cos (x1,y1) (x2,y2)

17
Rotation as a Matrix operation x2 x1*cos -y1*sin y2 x1*sin +y1*cos

18
Rotation by 90 degrees 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?? (0,0) (30,40) (60,80)

19
Rotation by 90 degrees x1p =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)

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 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 = 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

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google