Download presentation

Presentation is loading. Please wait.

Published byBrennen Meeks Modified over 2 years ago

1
18/2/00SEM107 - © Kamin & Reddy Class 7 - LineList - 1 Class 7 - Line Drawings The LineList data type r Recursive methods to construct line drawings

2
18/2/00SEM107 - © Kamin & Reddy Class 7 - LineList - 2 Line drawings It is possible to draw quite intricate pictures using only lines.

3
18/2/00SEM107 - © Kamin & Reddy Class 7 - LineList - 3 Graphics in Java r Can draw various shapes on the screen (we will see how next week) r Like most computer graphics systems, the window is divided into numbered pixels. r Pixels are numbered like this: (0,0) y increases x increases

4
18/2/00SEM107 - © Kamin & Reddy Class 7 - LineList - 4 Lines We will provide a class called Line, with several operations: Construct a line from (x0,y0) to (x1,y1) by: new Line(x0,y0,x1,y1) m Given a line L, find coordinates of the starting and ending points using instance methods x0(), y0(), x1(), y1(). Lines can be printed by System.out.println.

5
18/2/00SEM107 - © Kamin & Reddy Class 7 - LineList - 5 Line lists The class LL has the same operations as IL, but contains operations on lists of Line objects: m LL.nil LineList LL.cons(Line ln, LineList L) - construct a list containing ln at front Line hd () - return head of L LineList tl () - return tail of L m boolean empty ()

6
18/2/00SEM107 - © Kamin & Reddy Class 7 - LineList - 6 Creating line drawings We will “draw” figures made up of lines, in the sense that we will create LineList ’s containing all of the lines. r Next week we will see how to turn these lists of lines into actual drawings

7
18/2/00SEM107 - © Kamin & Reddy Class 7 - LineList - 7 Example: Drawing vertical lines Given these five arguments: int leng int x int y int incr int n draw n vertical lines, separated by incr horizontal distance. Each line is of length leng, and the first line is from ( x, y ) to ( x, y+leng ).

8
18/2/00SEM107 - © Kamin & Reddy Class 7 - LineList - 8 Example: Drawing vertical lines (cont.) For example, the call vertLines(50,0,0,10,3) produces the output (0,0,0,50),(10,0,10,50),(20,0,20,50) representing the drawing (0,0)

9
18/2/00SEM107 - © Kamin & Reddy Class 7 - LineList - 9 Example: Drawing vertical lines (cont.) As with all our recursive methods, we ask the question, how can a recursive call with “smaller” arguments help calculate the current call? Answer: if we can draw the first (leftmost) line, perhaps we can draw the rest of the lines recursively...

10
18/2/00SEM107 - © Kamin & Reddy Class 7 - LineList - 10 Drawing vertical lines (cont.) static LineList vertLines (int leng, int x, int y, int incr, int count) { if (count == 0) return LL.nil; else return LL.cons( new Line(x, y, x, y+leng), vertLines(leng, x+incr, y, incr, count-1)); }

11
18/2/00SEM107 - © Kamin & Reddy Class 7 - LineList - 11 Drawing lines at an angle To draw a line of length m at an angle theta from the x-axis: m y = m sin x = m cos (But remember, this is upside-down...)

12
18/2/00SEM107 - © Kamin & Reddy Class 7 - LineList - 12 Drawing “stars” Given n and r, draw n lines of length r, each starting at the origin and spaced evenly around it. E.g. star(6,50) :

13
18/2/00SEM107 - © Kamin & Reddy Class 7 - LineList - 13 Drawing “stars” (cont.) This is one of those cases where the method you want to define cannot be defined recursively directly. Instead, you need a helper function that has more arguments. Here, you can define LineList star1 (int order, int radius, double theta, double angleIncr)

14
18/2/00SEM107 - © Kamin & Reddy Class 7 - LineList - 14 Drawing “stars” (cont.) which draws order lines of length radius separated by angleIncr radians, with the first at angle theta from the x-axis. Given this, define star : static LineList star (int order, int radius) { return star1 (order, radius, 0.0, 2*Math.PI/order); }

15
18/2/00SEM107 - © Kamin & Reddy Class 7 - LineList - 15 Drawing “stars” (cont.) static LineList star1 (int order, int radius, double theta, double angleIncr) { if (order == 0) return LL.nil; else return LL.cons( new Line(0, 0, (int)Math.round(radius*Math.cos(theta)), (int)Math.round(radius*Math.sin(theta))), star1(order-1, radius, theta+angleIncr, angleIncr)); } (Detail: Math.cos and Math.sin take arguments in radians, instead of degrees.)

16
18/2/00SEM107 - © Kamin & Reddy Class 7 - LineList - 16 “Translating” shapes There is a problem with our drawing of stars: it produces lines with negative coordinates, which don’t exist in the Java window. Moving a shape is called translating it. In this case, we want to move the star down and to the right by radius points. A general translation method for LineList’s is easy to write recursively:

17
18/2/00SEM107 - © Kamin & Reddy Class 7 - LineList - 17 “Translating” shapes (cont.) static LineList translate (LineList L, int x, int y) { if (L.empty()) return L; else { Line ln = L.hd(); Line transLine = new Line(ln.x0()+x, ln.y0()+y, ln.x1()+x, ln.y1()+y); return LL.cons(transLine, translate(L.tl(), x, y)); }

18
18/2/00SEM107 - © Kamin & Reddy Class 7 - LineList - 18 “Translating” shapes (cont.) Add translation to the star method: static LineList star (int order, int radius) { LineList S = star1 (order, radius, 0.0, 2*Math.PI/order); return translate(S, radius, radius); }

19
18/2/00SEM107 - © Kamin & Reddy Class 7 - LineList - 19 Drawing polygons A polygon is like a star, except that the lines are drawn between the end-points of the lines, instead of being drawn from the center. We define polygon (int order, int radius) to draw a polygon with order sides and vertices a distance radius from the origin

20
18/2/00SEM107 - © Kamin & Reddy Class 7 - LineList - 20 Drawing polygons (cont.) static LineList polygon (int order, int radius) { return polygon1(order, radius, 0.0, 2*Math.PI/order); } Again, polygon cannot be computed directly, but can be computed using LineList polygon1 (int order, int radius, double theta, double angleincr) which draw order lines between points at distance radius from the origin, given also that the first line is at angle theta from the x-axis, and the lines are separated by angleincr radians.

21
18/2/00SEM107 - © Kamin & Reddy Class 7 - LineList - 21 Drawing polygons (cont.) static LineList polygon1 (int order, int radius, double theta, double angleincr) { if (order == 0) return LL.nil; else { int x0 = (int)(radius*Math.cos(theta)); int y0 = (int)(radius*Math.sin(theta)); int x1 = (int)(radius*Math.cos(theta+angleincr)); int y1 = (int)(radius*Math.sin(theta+angleincr)); return LL.cons(new Line(x0,y0,x1,y1), polygon1(order-1, radius, theta+angleincr, angleincr)); }

22
18/2/00SEM107 - © Kamin & Reddy Class 7 - LineList - 22 Drawing polygons (cont.) An unfortunate feature of polygon1 is that it calculates all four points on each recursive call, even though two of them have already been calculated on the previous call. Since sin and cos are fairly expensive, we can avoid this calculation by passing two of the points along to each call:

23
18/2/00SEM107 - © Kamin & Reddy Class 7 - LineList - 23 Drawing polygons (cont.) static LineList polygon1 (int order, int radius, double theta, double angleincr, int x0, int y0) { if (order == 0) return LL.nil; else { int x1 = (int)(radius*Math.cos(theta+angleincr)); int y1 = (int)(radius*Math.sin(theta+angleincr)); return LL.cons(new Line(x0,y0,x1,y1), polygon1(order-1, radius, theta+angleincr, angleincr, x1, y1)); }

24
18/2/00SEM107 - © Kamin & Reddy Class 7 - LineList - 24 Drawing polygons (cont.) Now, polygon must call polygon1 with two extra arguments: static LineList polygon (int order, int radius) { double theta = 2*Math.PI/order; int x0 = (int)(radius*Math.cos(theta)); int y0 = (int)(radius*Math.sin(theta)); return polygon1(order, radius, 0.0, theta, x0, y0); }

Similar presentations

OK

Trigonometry Math is beautiful. Is it? Well, if its not nice, than its damn sure of a very B.I.G. *HELP*.

Trigonometry Math is beautiful. Is it? Well, if its not nice, than its damn sure of a very B.I.G. *HELP*.

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on applied operations research analyst Ppt one hundred days of school Ppt on culture of rajasthan Ppt on biodegradable and non biodegradable photos Ppt on l&t finance stock price Ppt on regional rural banks in india Ppt on natural disasters for class 9 Ppt on multipurpose river valley projects Ppt on 5g technology Ppt on waves tides and ocean currents activity