summaryrefslogtreecommitdiffstats
path: root/voronoi/cPointi.java
blob: c538c2873c628b7be6328beac83a600f0b22b95d (plain) (blame)
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
package voronoi;
class cPointi {
	int x;

	int y;

	int z;

	cPointi() {
		x = y = z = 0;
	}

	cPointi(int x, int y) {
		this.x = x;
		this.y = y;
		this.z = 0;
	}

	cPointi(int x, int y, int z) {
		this.x = x;
		this.y = y;
		this.z = z;
	}

	public int Area2(cPointi a, cPointi b, cPointi c) {
		int area = ((c.x - b.x) * (a.y - b.y)) - ((a.x - b.x) * (c.y - b.y));
		return area;
	}

	public int AreaSign(cPointi a, cPointi b, cPointi c) {
		double area2;

		area2 = (b.x - a.x) * (double) (c.y - a.y) - (c.x - a.x)
				* (double) (b.y - a.y);

		if (area2 > 0.5)
			return 1;
		else if (area2 < -0.5)
			return -1;
		else
			return 0;
	}

	public boolean Left(cPointi a, cPointi b, cPointi c) {
		return AreaSign(a, b, c) > 0;
	}

	public boolean LeftOn(cPointi a, cPointi b, cPointi c) {
		return AreaSign(a, b, c) >= 0;
	}
	
	/*
	 * returns the distance of two points
	 */
	public double Dist(cPointi p, cPointi p1) {
		double l = Math.sqrt(Math.pow((p.x - p1.x), 2)
				+ Math.pow((p.y - p1.y), 2));
		return l;
	}

	public boolean Collinear(cPointi a, cPointi b, cPointi c) {
		return AreaSign(a, b, c) == 0;
	}

	public boolean Between(cPointi a, cPointi b, cPointi c) {
		if (!Collinear(a, b, c))
			return false;

		if (a.x != b.x)
			return ((a.x <= c.x) && (c.x <= b.x))
					|| ((a.x >= c.x) && (c.x >= b.x));
		else
			return ((a.y <= c.y) && (c.y <= b.y))
					|| ((a.y >= c.y) && (c.y >= b.y));
	}

	/*
	 * Returns the distance of the input point from its perp. proj. to the e1
	 * edge. Uses method detailed in comp.graphics.algorithms FAQ
	 */
	double DistEdgePoint(cPointi a, cPointi b, cPointi c) {
		double r, s;
		double length;
		double dproj = 0.0;
		length = Math.sqrt(Math.pow((b.x - a.x), 2) + Math.pow((b.y - a.y), 2));

		if (length == 0.0) {
			System.out.println("DistEdgePoint: Length = 0");
		}
		r = (((a.y - c.y) * (a.y - b.y)) - ((a.x - c.x) * (b.x - a.x)))
				/ (length * length);
		s = (((a.y - c.y) * (b.x - a.x)) - ((a.x - c.x) * (b.y - a.y)))
				/ (length * length);

		dproj = Math.abs(s * length);

		if ((s != 0.0) && ((0.0 <= r) && (r <= 1.0)))
			return dproj;
		if ((s == 0.0) && Between(a, b, c))
			return 0.0;
		else {
			double ca = Dist(a, c);
			double cb = Dist(b, c);
			return Math.min(ca, cb);
		}
	}
}