-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTriangle.java
More file actions
69 lines (58 loc) · 1.23 KB
/
Triangle.java
File metadata and controls
69 lines (58 loc) · 1.23 KB
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
package csg;
import java.awt.*;
class Triangle extends Geometry {
int x1, y1, x2, y2, x3, y3;
Triangle(Op op) {
super(op);
x1 = 40;
y1 = 20;
x2 = 20;
y2 = 40;
x3 = 60;
y3 = 40;
}
Triangle(Op op, int x1, int y1, int x2, int y2, int x3, int y3) {
super(op);
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
this.x3 = x3;
this.y3 = y3;
}
boolean isHit(int x, int y) {
boolean b0, b1, b2;
b0 = sign(x, y, x1, y1, x2, y2) < 0;
b1 = sign(x, y, x2, y2, x3, y3) < 0;
b2 = sign(x, y, x3, y3, x1, y1) < 0;
return (b0 == b1) && (b1 == b2);
}
public String toString() {
return "Triangle " + op.name();
}
void draw(Graphics g) {
g.setColor(color);
g.drawLine(x1, y1, x2, y2);
g.drawLine(x2, y2, x3, y3);
g.drawLine(x3, y3, x1, y1);
}
private double sign(int px1, int py1, int px2, int py2, int px3, int py3) {
return (px1 - px3) * (py2 - py3) - (px2 - px3) * (py1 - py3);
}
void corner(Graphics g, int corner) {
g.setColor(Color.WHITE);
switch (corner) {
case 1:
g.fillRect(x1 - 1, y1 - 1, 2, 2);
break;
case 2:
g.fillRect(x2 - 1, y2 - 1, 2, 2);
break;
case 3:
g.fillRect(x3 - 1, y3 - 1, 2, 2);
break;
default:
break;
}
}
}