Problem-solving using Divide and Conquer

 

 

We will list programs for a few of the mini projects.

Jeep:

 

We see the following components:

·         wheel

·         engine compartment

·         passenger compartment

 

The wheel is a circle with diameter as input.

ERASE "dcircle

TO dcircle :size

REPEAT 360 [

  FD 3.14159*:size/360

  RT 1

]

END

 

;Wheel: simply call the

;dcircle procedure

ERASE "wheel

TO wheel :size

  dcircle :size

END


The engine compartment is a rectangle:

 

ERASE "engine

TO engine :h :w

  REPEAT 2 [FD :h RT 90 FD :w RT 90]

END

 

 

The passenger compartment is a square:

 

ERASE "passenger

TO passenger :size

  REPEAT 4 [FD :size RT 90]

END

 

 

Using these components together, we get the jeep as follows:

 

Engine 50 100

PU RT 90 FD 100 LT 90 PD

Passenger 100

PU BK 25 RT 90 FD 25 LT 90 PD

wheel 50

PU LT 90 FD 100 RT 90 PD

wheel 50

PU FD 25 LT 90 FD 25 RT 90 PD

 

 

We put these instructions into a jeep procedure:

 

ERASE "jeep

TO jeep

  Engine 50 100

  PU RT 90 FD 100 LT 90 PD

  Passenger 100

  PU BK 25 RT 90 FD 25 LT 90 PD

  wheel 50

  PU LT 90 FD 100 RT 90 PD

  wheel 50

  PU FD 25 LT 90 FD 25 RT 90 PD

END


Finally, we add a scaling factor as input:

 

ERASE "jeep

TO jeep :m

  Engine 50*:m 100*:m

  PU RT 90 FD 100*:m LT 90 PD

  Passenger 100*:m

  PU BK 25*:m RT 90 FD 25*:m LT 90 PD

  wheel 50*:m

  PU LT 90 FD 100*:m RT 90 PD

  wheel 50*:m

  PU FD 25*:m LT 90 FD 25*:m RT 90 PD

END

 

 

We test this procedure to make sure it works. We use the jump procedure (defined earlier in the chapter) to draw 3 jeeps in a row.

 

jump -300 0 jeep 1 jump 300 0 jeep 0.5

jump 200 0 jeep 0.25 HT

 

 

 

 

 

 

 

 

 

 

Traffic Light:

 

 

We see the following components:

·         lamp (3 of them)

·         casing for the 3 lamps

·         pole

 

 

 

 

ß Green light

 

 

 

ß Yellow light

 

 

ß Red light

Component lamp:

Each lamp consists of two circles: a colored circle on top of a black circle.

 

We will use the dcircle procedure that takes diameter as input:

ERASE "dcircle

TO dcircle :size

  REPEAT 360 [FD 3.14159*:size/360 RT 1]

END

 

Next, we define a procedure called f.dcircle that will draw a filled circle.

 

ERASE "f.dcircle

TO f.dcircle :D

  dcircle :D

  PU RT 90 FD :D/2 PD FILL BK :D/2 LT 90

END

 

We test it to confirm that it can draw filled circles of any width (diameter) and color (we have to call SETPC before calling the procedure).

 

This draws the outer black circle:

 

HT SETBG 7 SETPC 0 f.dcircle 100

 

The next 3 instructions draw the inner green circle:

PU RT 90 FD 10 LT 90 PD

SETPC 2 f.dcircle 80

PU RT 90 BK 10 LT 90 PD

 

 

Let's put all these instructions into a procedure:

 

ERASE "lamp

TO lamp

  SETPC 0 f.dcircle 100

  PU RT 90 FD 10 LT 90 PD

  SETPC 2 f.dcircle 80

  PU RT 90 BK 10 LT 90 PD

END

 

Next, we add 2 inputs to this procedure: one for the inner color, and one for the size of the lamp. For size, we use a scaling multiplier as usual.

 

ERASE "lamp

TO lamp :c :m

  SETPC 0 f.dcircle 100*:m

  PU RT 90 FD 10*:m LT 90 PD

  SETPC :c f.dcircle 80*:m

  PU RT 90 BK 10*:m LT 90 PD

END

 

 

 

 

 

We test it to confirm that it can draw lamps of any width and color.

lamp 11 1 jump 150 0 lamp 14 1.5 jump 180 0 lamp 4 0.5 HT

Component casing:

The 3 lamps are housed inside a colored rectangular casing. So, all we need is a procedure for filled rectangle:

 

ERASE "frectangle

TO frectangle :h :w

  REPEAT 2 [

    FD :h RT 90 FD :w RT 90

  ]

  PU FD :h/2 RT 90 FD :w/2 PD FILL

  BK :w/2 LT 90 BK :h/2

END

 

ERASE "casing

TO casing :h :w :c

  SETPC :c

  Frectangle :h :w

END

 

We test it to confirm that it can draw casing of any size and color.

 

casing 25 50 0 jump 150 0 casing 100 50 9 HT

 

 

 

 

 

Component pole:

The pole is identical to the casing. We can just reuse the same procedure. No extra work needed!

 

Putting things together:

After a bit of design work in our notebook, we know that we will first draw the pole, then the casing, and then each lamp inside. We also discover that the width of the casing must be the same as the width of each lamp, and the height of the casing would be 3 times its width (since it will house 3 lamps).

 

We use the following dimensions:

Width of the casing = width of each lamp = 50

Height of casing = 3*50 = 150

Height of pole = 100

Width of pole = 20

 

The instructions and the resulting traffic light are shown below:

 

 

Casing 100 20 11

PU FD 100 LT 90

FD 15 RT 90 PD

Casing 150 50 11

PU FD 25 PD

Lamp 4 0.5

PU FD 50 PD

Lamp 6 0.5

PU FD 50 PD

Lamp 2 0.5

PU BK 125 PD

 

ß Green light

 

 

ß Yellow light

 

 

ß Red light

 

 

 

 

 

We put these instructions inside a procedure called traffic.light:

 

ERASE "traffic.light

TO traffic.light

  Casing 100 20 11

  PU FD 100 LT 90 FD 15 RT 90 PD

  Casing 150 50 11

  PU FD 25 PD

  Lamp 4 0.5

  PU FD 50 PD

  Lamp 6 0.5

  PU FD 50 PD

  Lamp 2 0.5

  PU BK 125 PD

END

 

And finally, we add a scale multiplier to be able draw traffic lights of any size.

 

ERASE "traffic.light

TO traffic.light :m

  Casing 100*:m 20*:m 11

  PU FD 100*:m LT 90 FD 15*:m RT 90 PD

  Casing 150*:m 50*:m 11

  PU FD 25*:m PD

  Lamp 4 0.5*:m

  PU FD 50*:m PD

  Lamp 6 0.5*:m

  PU FD 50*:m PD

  Lamp 2 0.5*:m

  PU BK 125*:m PD

END

 

 

 

 

 

 

 

 

 

 

Let's test our final program:

 

traffic.light 1 jump 150 -80

traffic.light 0.75 jump 120 -50

traffic.light 0.5 HT