1 / 24

Class 7 - Line Drawings

Class 7 - Line Drawings. The LineList data type Recursive methods to construct line drawings. Line drawings. It is possible to draw quite intricate pictures using only lines. Graphics in Java. Can draw various shapes on the screen (we will see how next week)

isabel
Télécharger la présentation

Class 7 - Line Drawings

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. Class 7 - Line Drawings • The LineList data type • Recursive methods to construct line drawings

  2. Line drawings It is possible to draw quite intricate pictures using only lines.

  3. Graphics in Java • Can draw various shapes on the screen (we will see how next week) • Like most computer graphics systems, the window is divided into numbered pixels. • Pixels are numbered like this: (0,0) x increases y increases

  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) • 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. Line lists The class LL has the same operations as IL, but contains operations on lists of Line objects: • 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 • boolean empty ()

  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. • Next week we will see how to turn these lists of lines into actual drawings

  7. Example: Drawing vertical lines Given these five arguments: • int leng • int x • int y • int incr • int n draw nvertical 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. 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. 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. 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. 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. 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. 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. 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. 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. “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. “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. “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. 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. 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. 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. 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. 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. 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); }

More Related