24
22/2/00 SEM107 © Kamin & Reddy Class 8 - Hilbert’s curve - 1 Class 8 - Recursive Pictures Recursively-defined curves The Hilbert curve

Class 8 - Recursive Pictures

  • Upload
    tola

  • View
    22

  • Download
    0

Embed Size (px)

DESCRIPTION

Class 8 - Recursive Pictures. Recursively-defined curves The Hilbert curve. Recursive drawings. Many shapes are “self-similar” - their overall structure is repeated in sub-pictures. Peano curve:. Recursive drawings (cont.). “Koch snowflake”:. Recursive drawings (cont.). - PowerPoint PPT Presentation

Citation preview

Page 1: Class 8 - Recursive Pictures

22/2/00 SEM107 © Kamin & Reddy Class 8 - Hilbert’s curve - 1

Class 8 - Recursive Pictures

Recursively-defined curvesThe Hilbert curve

Page 2: Class 8 - Recursive Pictures

22/2/00 SEM107 © Kamin & Reddy Class 8 - Hilbert’s curve - 2

Recursive drawings

Many shapes are “self-similar” - their overall structure is repeated in sub-pictures.

Peano curve:

Page 3: Class 8 - Recursive Pictures

22/2/00 SEM107 © Kamin & Reddy Class 8 - Hilbert’s curve - 3

Recursive drawings (cont.)

“Koch snowflake”:

Page 4: Class 8 - Recursive Pictures

22/2/00 SEM107 © Kamin & Reddy Class 8 - Hilbert’s curve - 4

Recursive drawings (cont.)

Page 5: Class 8 - Recursive Pictures

22/2/00 SEM107 © Kamin & Reddy Class 8 - Hilbert’s curve - 5

The Hilbert Curve

Created by David Hilbert (1862-1943), this is a “space-filling” curve.

Hilbert curve of order n is constructed from four copies of the Hilbert curve of order n-1, properly oriented and connected.

Page 6: Class 8 - Recursive Pictures

22/2/00 SEM107 © Kamin & Reddy Class 8 - Hilbert’s curve - 6

Hilbert curves of order 1 & 2

HC(1):

HC(2):

Page 7: Class 8 - Recursive Pictures

22/2/00 SEM107 © Kamin & Reddy Class 8 - Hilbert’s curve - 7

Hilbert curve of order 3

HC(3):

Page 8: Class 8 - Recursive Pictures

22/2/00 SEM107 © Kamin & Reddy Class 8 - Hilbert’s curve - 8

Hilbert curve of order 4

Page 9: Class 8 - Recursive Pictures

22/2/00 SEM107 © Kamin & Reddy Class 8 - Hilbert’s curve - 9

Hilbert curve of order 5

Page 10: Class 8 - Recursive Pictures

22/2/00 SEM107 © Kamin & Reddy Class 8 - Hilbert’s curve - 10

The Hilbert Curve Pattern

Hilbert curves have “open” side on left:

Form H.C. of order n by combining four copies of H.C. of order n-1,

plus three connecting

lines (of length sl):

n-1

n-1

n-1n-1

n-1

Page 11: Class 8 - Recursive Pictures

22/2/00 SEM107 © Kamin & Reddy Class 8 - Hilbert’s curve - 11

Recursive definition of H.C.

This leads to the basic recursive pattern for defining HC(order, sl):

int diam = size of HC of order n-1;hcsub1 = HC(order-1, sl);hcul = rotate hcsub1 by -90 degrees;hcur = translate hcsub1 to (diam+sl, 0);hclr = translate hcsub1 to (diam+sl, diam+sl);hcll = rotate hcsub1 by 90 degrees, then translate to (0, diam+sl);hc = append(hcul, append(hcur, append(hclr, hcll)));return hc, with three lines added;

Page 12: Class 8 - Recursive Pictures

22/2/00 SEM107 © Kamin & Reddy Class 8 - Hilbert’s curve - 12

Translation

We have already seen how to translate a LineList:

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

Page 13: Class 8 - Recursive Pictures

22/2/00 SEM107 © Kamin & Reddy Class 8 - Hilbert’s curve - 13

Rotation

Rotation is more complicated. Consider rotating one point around the origin by angle :

1. Calculate m and m = x2+y2

= tan-1(y/x)2. = + 3. (x’,y’) = point of length m, at angle

(x,y)

(x’,y’)

Page 14: Class 8 - Recursive Pictures

22/2/00 SEM107 © Kamin & Reddy Class 8 - Hilbert’s curve - 14

Rotation (cont.)

We’ll rotate shapes (i.e. LineList’s) about theorigin by rotating each line:

static LineList rotateShapeAboutOrigin (LineList L, double theta) { if (L.empty()) return LL.nil; else return LL.cons(rotateLine(L.hd(), theta), rotateShapeAboutOrigin(L.tl(), theta)); }

Page 15: Class 8 - Recursive Pictures

22/2/00 SEM107 © Kamin & Reddy Class 8 - Hilbert’s curve - 15

Rotation (cont.)

Rotating individual lines around the origin is a matter of rotating both endpoints, as we have indicated. Some added complexity comes from two factors: Math.atan returns angles in the range -/2

to /2 (i.e. only angles in the right half-plane) The graphics coordinate system is “upside-down”

relative to ordinary Cartesian coordinates.

Page 16: Class 8 - Recursive Pictures

22/2/00 SEM107 © Kamin & Reddy Class 8 - Hilbert’s curve - 16

Rotation (cont.)

static Line rotateLine (Line ln, double theta) { int x0 = ln.x0(), y0 = -ln.y0(), // turn coordinates rightside-up x1 = ln.x1(), y1 = -ln.y1(); // turn coordinates rightside-up double currangle0 = Math.atan((double)y0/x0); double newangle0 = currangle0+theta; if (x0<0) newangle0 = newangle0 + Math.PI; double mag0 = Math.sqrt(x0*x0+y0*y0); int newx0 = (int)Math.round(mag0*Math.cos(newangle0)); int newy0 = -(int)Math.round(mag0*Math.sin(newangle0));

Page 17: Class 8 - Recursive Pictures

22/2/00 SEM107 © Kamin & Reddy Class 8 - Hilbert’s curve - 17

Rotation (cont.)

double currangle1 = Math.atan((double)y1/x1);double newangle1 = currangle1+theta;if (x1<0) newangle1 = newangle1 + Math.PI;double mag1 = Math.sqrt(x1*x1+y1*y1);int newx1 = (int)Math.round(mag1*Math.cos(newangle1));int newy1 = -(int)Math.round(mag1*Math.sin(newangle1));return new Line(newx0,newy0,newx1,newy1);}

Page 18: Class 8 - Recursive Pictures

22/2/00 SEM107 © Kamin & Reddy Class 8 - Hilbert’s curve - 18

Rotation (cont.)

One remaining technicality: when we say “rotate a shape”, we usually mean, “rotate it around its center”. However, so far we know only how to rotate a shape around the origin. The rotateShape method takes a shape and an angle and a point (x,y) which is taken to be the center of the shape.

Page 19: Class 8 - Recursive Pictures

22/2/00 SEM107 © Kamin & Reddy Class 8 - Hilbert’s curve - 19

Rotation (cont.)

static LineList rotateShape (LineList L, double theta, int x, int y) { LineList transL = translate(L, -x, -y); LineList rotateL = rotateShapeAboutOrigin(transL, theta); return translate(rotateL, x, y); }

It does so by translating the shape, then rotating,then translating back:

Page 20: Class 8 - Recursive Pictures

22/2/00 SEM107 © Kamin & Reddy Class 8 - Hilbert’s curve - 20

The Hilbert Curve

int diam = size of HC of order n-1;hcsub1 = HC(order-1, sl);hcul = rotate hcsub1 by -90 degrees;hcur = translate hcsub1 to (diam+sl, 0);hclr = translate hcsub1 to (diam+sl, diam+sl);hcll = rotate hcsub1 by 90 degrees, then translate to (0, diam+sl);hc = append(hcul, append(hcur, append(hclr, hcll)));return hc, with three lines added;

Recall again the abstract version of HC(order, sl):

Page 21: Class 8 - Recursive Pictures

22/2/00 SEM107 © Kamin & Reddy Class 8 - Hilbert’s curve - 21

Hilbert Curve (cont.)

We can now say that the rotation steps shouldrotate the shape around the center of the Hilbertcurve of order n-1. That center is at (diam/2,diam/2).How do we calculate diam? A review of theHilbert curve of various orders show thatHC(n-1) has diameter ( 2n-1-1)*sl.This leads to our solution:

Page 22: Class 8 - Recursive Pictures

22/2/00 SEM107 © Kamin & Reddy Class 8 - Hilbert’s curve - 22

The HC Method

static LineList HC (int order, int sl) { if (order == 1) return LL.cons(new Line(0,0,sl,0), LL.cons(new Line(sl,0,sl,sl), LL.cons(new Line(sl,sl,0,sl), LL.nil))); else { int diam = sl*(int)(Math.pow(2,order-1)-1); // diameter of HC(order-1) int radius = diam/2; LineList hcsub1 = HC(order-1, sl);

Page 23: Class 8 - Recursive Pictures

22/2/00 SEM107 © Kamin & Reddy Class 8 - Hilbert’s curve - 23

The HC Method (cont.)

LineList hcul = rotateShape(hcsub1, -Math.PI/2, radius, radius);LineList hcur = translate(hcsub1, diam+sl, 0);LineList hclr = translate(hcsub1, diam+sl, diam+sl);LineList hcll = translate( rotateShape(hcsub1,Math.PI/2, radius, radius), 0, diam+sl);LineList hc = append(hcul, append(hcur, append(hclr, hcll)));

Page 24: Class 8 - Recursive Pictures

22/2/00 SEM107 © Kamin & Reddy Class 8 - Hilbert’s curve - 24

The HC Method (cont.)

hc = LL.cons(new Line(diam,0,diam+sl,0), LL.cons(new Line(diam+sl,diam,diam+sl,diam+sl), LL.cons(new Line(diam+sl,2*diam+sl, diam,2*diam+sl), hc))); return hc; }}