Blame


1 12687dd9 2023-08-04 jrmu ;; The first three lines of this file were inserted by DrScheme. They record metadata
2 12687dd9 2023-08-04 jrmu ;; about the language level of this file in a form that our tools can easily process.
3 12687dd9 2023-08-04 jrmu #reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname |27.1|) (read-case-sensitive #t) (teachpacks ((lib "draw.ss" "teachpack" "htdp") (lib "arrow.ss" "teachpack" "htdp") (lib "gui.ss" "teachpack" "htdp"))) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ((lib "draw.ss" "teachpack" "htdp") (lib "arrow.ss" "teachpack" "htdp") (lib "gui.ss" "teachpack" "htdp")))))
4 12687dd9 2023-08-04 jrmu ;The minimum area before the approximating triangle of the Bezier curve is drawn.
5 12687dd9 2023-08-04 jrmu (define MINAREA 10)
6 12687dd9 2023-08-04 jrmu
7 12687dd9 2023-08-04 jrmu ;bezier : posn posn posn symbol -> true
8 12687dd9 2023-08-04 jrmu ;Draw a smooth curve from p1 to p3, viewed from p2. In the trivial case, where the area of the triangle is very small, simply draw the triangle. When the triangle is not small, partition the larger triangle into two smaller triangles, where the first triangle is composed of p1, the midpoint of p1 and p2 (termed r2), and the midpoint between r2 and the midpoint between p2 and p3 (termed q2). The second triangle is composed of p3, q2, and the midpoint between r2 and q2. Repeat bezier on these successive triangles.
9 12687dd9 2023-08-04 jrmu
10 12687dd9 2023-08-04 jrmu (define (bezier p1 p2 p3 color)
11 12687dd9 2023-08-04 jrmu (local ((define r2 (midpoint p1 p2))
12 12687dd9 2023-08-04 jrmu (define q2 (midpoint p2 p3))
13 12687dd9 2023-08-04 jrmu (define m (midpoint r2 q2)))
14 12687dd9 2023-08-04 jrmu (cond
15 12687dd9 2023-08-04 jrmu [(small-enough? p1 p2 p3) (draw-triangle p1 p2 p3 color)]
16 12687dd9 2023-08-04 jrmu [else (and (bezier p1 r2 m color)
17 12687dd9 2023-08-04 jrmu (bezier m q2 p3 color))])))
18 12687dd9 2023-08-04 jrmu
19 12687dd9 2023-08-04 jrmu ;small-enough? : posn posn posn -> boolean
20 12687dd9 2023-08-04 jrmu ;Determine if the triangle is small enough to draw.
21 12687dd9 2023-08-04 jrmu
22 12687dd9 2023-08-04 jrmu (define (small-enough? a b c)
23 12687dd9 2023-08-04 jrmu (< (area-of-triangle a b c) MINAREA))
24 12687dd9 2023-08-04 jrmu
25 12687dd9 2023-08-04 jrmu ;distance : posn posn -> number
26 12687dd9 2023-08-04 jrmu ;Given p1, p2, determine the distance between two points.
27 12687dd9 2023-08-04 jrmu
28 12687dd9 2023-08-04 jrmu (define (distance p1 p2)
29 12687dd9 2023-08-04 jrmu (sqrt (+ (sqr (- (posn-x p2) (posn-x p1)))
30 12687dd9 2023-08-04 jrmu (sqr (- (posn-y p2) (posn-y p1))))))
31 12687dd9 2023-08-04 jrmu
32 12687dd9 2023-08-04 jrmu ;midpoint : posn posn -> posn
33 12687dd9 2023-08-04 jrmu ;Given a, b, find the midpoint of the two posns.
34 12687dd9 2023-08-04 jrmu
35 12687dd9 2023-08-04 jrmu (define (midpoint a b)
36 12687dd9 2023-08-04 jrmu (make-posn (/ (+ (posn-x a) (posn-x b)) 2)
37 12687dd9 2023-08-04 jrmu (/ (+ (posn-y a) (posn-y b)) 2)))
38 12687dd9 2023-08-04 jrmu
39 12687dd9 2023-08-04 jrmu ;draw-triangle : posn posn posn -> true
40 12687dd9 2023-08-04 jrmu ;Draw the triangle that contains a, b, and c as vertices.
41 12687dd9 2023-08-04 jrmu
42 12687dd9 2023-08-04 jrmu (define (draw-triangle a b c color)
43 12687dd9 2023-08-04 jrmu (and (draw-solid-line a b color)
44 12687dd9 2023-08-04 jrmu (draw-solid-line b c color)
45 12687dd9 2023-08-04 jrmu (draw-solid-line c a color)))
46 12687dd9 2023-08-04 jrmu
47 12687dd9 2023-08-04 jrmu ;area-of-triangle : posn posn posn -> number
48 12687dd9 2023-08-04 jrmu ;Given a, b, c, determine the area of the triangle. (uses Heron's formula)
49 12687dd9 2023-08-04 jrmu
50 12687dd9 2023-08-04 jrmu (define (area-of-triangle a b c)
51 12687dd9 2023-08-04 jrmu (local ((define A (distance b c))
52 12687dd9 2023-08-04 jrmu (define B (distance a c))
53 12687dd9 2023-08-04 jrmu (define C (distance a b))
54 12687dd9 2023-08-04 jrmu (define semiperimeter (/ (+ A B C) 2)))
55 12687dd9 2023-08-04 jrmu (sqrt (* semiperimeter
56 12687dd9 2023-08-04 jrmu (- semiperimeter A)
57 12687dd9 2023-08-04 jrmu (- semiperimeter B)
58 12687dd9 2023-08-04 jrmu (- semiperimeter C)))))
59 12687dd9 2023-08-04 jrmu
60 12687dd9 2023-08-04 jrmu (define x1 (make-posn 300 300))
61 12687dd9 2023-08-04 jrmu (define x2 (make-posn 400 500))
62 12687dd9 2023-08-04 jrmu (define x3 (make-posn 500 200))
63 12687dd9 2023-08-04 jrmu
64 12687dd9 2023-08-04 jrmu (define p1 (make-posn 50 50))
65 12687dd9 2023-08-04 jrmu (define p2 (make-posn 150 150))
66 12687dd9 2023-08-04 jrmu (define p3 (make-posn 250 100))
67 12687dd9 2023-08-04 jrmu
68 12687dd9 2023-08-04 jrmu (start 1000 1000)
69 12687dd9 2023-08-04 jrmu (bezier x1 x2 x3 'blue)
70 12687dd9 2023-08-04 jrmu (bezier p1 p2 p3 'purple)