68
1
/*
2
Implicit Line and Circle
3
This example uses an implicit representations of
4
a line and a circle to draw a picture. 5000 circles
5
are placed at random locations. Points inside a specific
6
circle are colored green. Points above the line are
7
colored red. Points below the line are colored blue.
8
9
Colors include transparency to give a blending effect.
10
*/
11
12
13
/*
14
This function uses the imlicit definition of a line
15
(y-y1)*(x2-x1)-(x-x1)*(y2-y1)=0
16
connecting (x1,y1) and (x2,y2). A positive return
17
value puts (x,y) on one side of the line. A negative
18
value puts it on the other.
19
*/
20
function impLine(x,y,x1,y1,x2,y2){
21
return((y-y1)*(x2-x1)-(x-x1)*(y2-y1));
22
}
23
24
/*
25
This function uses the implicit definition of a circle
26
(x-x1)*(x-x1)+(y-y1)*(y-y1)-r*r=0
27
centered at (x1,y1) with radius r. A positive return
28
value means that (x,y) is outside of the circle. A
29
negative value puts (x,y) inside the circle.
30
*/
31
function impCircle(x,y,x1,y1,r){
32
return((x-x1)*(x-x1)+(y-y1)*(y-y1)-r*r);
33
}
34
35
function setup(){
36
color(0,0,0,0); //this makes the outline of the
37
//circles invisible
38
}
39
function draw(){
40
var x,y; //coordinates of circle
41
var d; //value from LINE function
42
var e; //value for CIRCLE function
43
var i; //index for for loop
44
45
//repeat 5000 times
46
for (i=0;i<5000;i=i+1){
47
//random point
48
x=width*random();
49
y=height*random();
50
51
//we call impLine with the line
52
//connecting (0,300) and (400,100)
53
d=impLine(x,y,0,300,400,100);
54
55
//call impCircle with circle
56
//at (200,200) with radius 100
57
e=impCircle(x,y,200,200,100);
58
if (e<0) //inside circle
59
fillcolor(0,255,0,0.1)
60
else if (d<0) //above the line
61
fillcolor(255,0,0,0.1);
62
else //below or on the line
63
fillcolor(0,0,255,0.1);
64
circle(x,y,10);
65
fill();
66
}
67
}
68