aboutsummaryrefslogtreecommitdiff
path: root/triangles.c
blob: 320807fe7a8192536b0998a8dca5fefa970e16f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#include <u.h>
#include <libc.h>
#include <draw.h>
#include <event.h>
#include <keyboard.h>

typedef struct Point2 Point2;
typedef struct Triangle Triangle;
typedef struct Circle Circle;
typedef struct Object Object;

struct Point2
{
	double x, y, w;
};

struct Triangle
{
	Point2 p0, p1, p2;
};

struct Circle
{
	Point2 c;
	double r;
};

struct Object
{
	Triangle t;
	Circle c;
};

enum {
	Cbg,
	Ctri,
	Ccir,
	NCOLOR
};

Image *pal[NCOLOR];
Object *tris;
ulong ntri;
Point2 pts[3];
int npt;

Point2
Pt2(double x, double y, double w)
{
	return (Point2){x,y,w};
}

Point2
addpt2(Point2 p, Point2 q)
{
	return (Point2){p.x+q.x,p.y+q.y,p.w+q.w};
}

Point2
subpt2(Point2 p, Point2 q)
{
	return (Point2){p.x-q.x,p.y-q.y,p.w-q.w};
}

Point2
mulpt2(Point2 p, double s)
{
	return (Point2){p.x*s,p.y*s,p.w*s};
}

Point2
divpt2(Point2 p, double s)
{
	return (Point2){p.x/s,p.y/s,p.w/s};
}

double
veclen(Point2 p)
{
	return sqrt(p.x*p.x + p.y*p.y);
};

Point2
crossvec(Point2 p, Point2 q)
{
	return (Point2){
		p.y*q.w - p.w*q.y,
		p.w*q.x - p.x*q.w,
		p.x*q.y - p.y*q.x
	};
}

void
addobj(Triangle t, Circle c)
{
	tris = realloc(tris, ++ntri*sizeof(Object));
	tris[ntri-1].t = t;
	tris[ntri-1].c = c;
}

double
triperi(Triangle t)
{
	return veclen(subpt2(t.p0, t.p1))
		+ veclen(subpt2(t.p1, t.p2))
		+ veclen(subpt2(t.p2, t.p0));
};

double
triarea(Triangle t)
{
	Point2 p;
	double r;

	p = crossvec(t.p0, t.p1);
	p = addpt2(p, crossvec(t.p1, t.p2));
	p = addpt2(p, crossvec(t.p2, t.p0));
	r = sqrt(p.x*p.x + p.y*p.y + p.w*p.w);
	return r/2;
}

Point
toscreen(Point2 p)
{
	return addpt(screen->r.min, Pt(p.x,p.y));
}

Point2
fromscreen(Point p)
{
	p = subpt(p, screen->r.min);
	return (Point2){p.x,p.y,0};
}

void
triangle(Triangle t)
{
	Point p[4];

	p[0] = toscreen(t.p0);
	p[1] = toscreen(t.p1);
	p[2] = toscreen(t.p2);
	p[3] = p[0];
	poly(screen, p, nelem(p), Endsquare, Endsquare, 0, pal[Ctri], ZP);
}

void
circle(Circle c)
{
	ellipse(screen, toscreen(c.c), c.r, c.r, 0, pal[Ccir], ZP);
}

void
initpalette(void)
{
	pal[Cbg] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, 0x000000ff);
	pal[Ctri] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, 0x00ff00ff);
	pal[Ccir] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, 0xff0000ff);
}

void
redraw(void)
{
	int i;

	draw(screen, screen->r, pal[Cbg], nil, ZP);
	for(i = 0; i < ntri; i++){
		triangle(tris[i].t);
		circle(tris[i].c);
	}
	for(i = 0; i < npt; i++)
		fillellipse(screen, toscreen(pts[i]), 2, 2, pal[Ctri], ZP);
	flushimage(display, 1);
}

void
usage(void)
{
	fprint(2, "usage: %s\n", argv0);
	exits("usage");
}

void
main(int argc, char *argv[])
{
	Event e;
	Triangle t;
	Circle c;

	ARGBEGIN{
	default: usage();
	}ARGEND;
	if(argc > 0)
		usage();
	if(initdraw(nil, nil, nil) < 0)
		sysfatal("initdraw: %r");
	initpalette();
	einit(Emouse|Ekeyboard);
	redraw();
	for(;;)
		switch(event(&e)){
		case Emouse:
			if((e.mouse.buttons&1) != 0){
				pts[npt++] = fromscreen(e.mouse.xy);
				if(npt >= nelem(pts)){
					t.p0 = pts[0];
					t.p1 = pts[1];
					t.p2 = pts[2];
					c.c = divpt2(addpt2(addpt2(mulpt2(t.p0, veclen(subpt2(t.p1, t.p2))),
						mulpt2(t.p1, veclen(subpt2(t.p2, t.p0)))),
						mulpt2(t.p2, veclen(subpt2(t.p0, t.p1)))),
					triperi(t));
					c.r = 2*triarea(t)/triperi(t);
					addobj(t, c);
					npt = 0;
				}
				redraw();
			}
			break;
		case Ekeyboard:
			switch(e.kbdc){
			case 'q':
			case Kdel:
				exits(0);
			}
			break;
		}
}

void
eresized(int)
{
	if(getwindow(display, Refnone) < 0)
		sysfatal("resize failed");
	redraw();
}