Title: Graohics
1Graohics
- CSC 171 FALL 2001
- LECTURE 16
2History 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.
3Coordinate System
- Origin in upper left
- X increases to right
- Y increases down
Increasing x
(0,0)
Increasing y
4Coordinate System
- So
- g.drawLine(30,40,50,70)
Increasing x
(0,0)
(30,40)
(50,70)
Increasing y
5Basic Graphics Operations
- Translation
- Change the position
- Scale
- Change the size
- Rotation
- Change the angle
6Translation
- /Moving an object is addition /
- int x1,y1,x2,y2
- x130 y140 x260 y280
- g.drawLine(x1,y1,x2,y2)
- //20 to the left , 10 down
- x120 x220
- y1 10 y210
- g.drawLine(x1,y1,x2,y2)
Increasing x
(0,0)
(30,40)
(50,50)
(80,90)
(60,80)
Increasing y
7Scale
- /Sizing an object multiplication /
- int x1,y1,x2,y2
- x130 y140 x260 y280
- g.drawLine(x1,y1,x2,y2)
- //double the size
- x12 x22
- y1 2 y22
- g.drawLine(x1,y1,x2,y2)
- // NOT in place
Increasing x
(0,0)
(30,40)
(60,80)
(60,80)
Increasing y
(120,160)
8Question
- Given scale and translation as above
- How would you do translation in place?
- (ie keep center of line in place, alter size)
(15,20)
(30,40)
(60,80)
(75,100)
9Scale in place
- int x1,y1,x2,y2
- x130 y140 x260 y280
- g.drawLine(x1,y1,x2,y2)
- //center point
- int averageX (x1x2)/2
- Int averageY (y1y2)/2
- //put center _at_ origin
- x1-averageX
- x2-averageX
- y1-averageY
- y2-averageY
- //scale as before
- x12 x22
- y1 2 y22
- //center is still at center
- //so, return center to same
- //put center _at_ origin
- x1averageX
- x2averageX
- x1averageY
- y2averageY
- g.drawLine(x1,y1,x2,y2)
10Scale in place
- //scale as before
- x12 // -15 2 -30
- x22 // 15 2 30
- y1 2 // -20 2 -40
- y22 // 20 2 40
- //center is still at center
- //so, return center to same
- //put center _at_ origin
- x1averageX // -30 45 15
- x2averageX // 3045 75
- x1averageY // -4060 20
- y2averageY // 4060 100
- g.drawLine(x1,y1,x2,y2)
- int x1,y1,x2,y2
- x130 y140 x260 y280
- g.drawLine(x1,y1,x2,y2)
- //center point
- int averageX (x1x2)/2 //45
- Int averageY (y1y2)/2 //60
- //put center _at_ origin
- x1-averageX // 30-45 -15
- x2-averageX // 60-45 15
- y1-averageY // 40-60 -20
- y2-averageY // 80-60 20
11Rotation
- 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
12A little trigonometry
- Recall
- On the unit circle,
- Give the angle F
- xcos(F)
- ysin(F)
(0,-1)
x
F
(1,0)
(-1,0)
y
(x,y)
(0,1)
ysin(F)
xcos(F)
13Any point in the plane
- xrcos(F)
- yrsin(F)
- Rotation about the origin is going from one point
on the circle to another - (adding an angle)
(0,-r)
x
F
(r,0)
(-r,0)
y
(x,y)
(0,r)
yrsin(F)
xrcos(F)
14Angle Addition
- Going from (x1,y1)
- add f to a
- is like finding the sum
- Remember
- cos(af)
- cosacosf-sinasinf
- sin(af)
- sinacosfcosasinf
a
f
(x1,y1)
(x2,y2)
15Angle Addition
- x1rcos(a)
- y1rsin(a)
- x2 rcos(af)
- r (cosacosf-sinasinf)
- x1cosf-y1sinf
- y2 r sin(af)
- r(sinacosfcosasinf) x1sinfy1cosf
a
f
(x1,y1)
(x2,y2)
16Rotation about the origin
- x2 x1cosf-y1sinf
- y2 x1sinay1cosf
a
f
(x1,y1)
(x2,y2)
17Rotation as a Matrix operation
- x2 x1cosf-y1sinf
- y2 x1sinay1cosf
18Rotation by 90 degrees
- int x1,y1,x2,y2
- x130 y140 x260 y280
- 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)
19Rotation by 90 degrees
- x1p x10-y11 // -40
- y1p y10x11// 30
- x2p x20-y21 //-80
- y2p y20x21 //60
(0,0)
(-40,30)
(30,40)
(-80,60)
(60,80)
20Rotation about the Origin
- Not In Place
- How do we do in place?
21Rotation 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
22FILE IO
- FileReader class
- read() method gets a character
- FileWriter class
- write() method writes a character
23 The Caesar Cipher
24Program Crypt.java import java.io.FileReader imp
ort 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 lt 2
args.length gt 4) usage() // gather
command line arguments and open files
25try for(int i 0 i lt args.length i)
if (argsi.substring(0, 1).equals("-")) //
it is a command line option String
option argsi.substring(1, 2) if
(option.equals("d")) decrypt true
else if (option.equals("k"))
key Integer.parseInt
(argsi.substring(2)) if (key lt 1
key gt NLETTERS) usage()
else if (infile
null) infile new FileReader(argsi)
else if (outfile null)
outfile new FileWriter(argsi)
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. _at_param c the character to encrypt
_at_param k the encryption key _at_return the
encrypted character / public static char
encrypt(char c, int k) if ('a lt c c lt
'z') return (char)('a (c - 'a k)
NLETTERS) if ('A lt c c lt 'Z')
return (char)('A (c - 'A k) NLETTERS)
return c
29/ Encrypts all characters in a file.
_at_param in the plaintext file _at_param out the
file to store the encrypted characters _at_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
30Whole class
31Passing class
32All exams projects in 73/134
33All exam projects in 73/134