-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathline.java
More file actions
74 lines (73 loc) · 2.26 KB
/
Copy pathline.java
File metadata and controls
74 lines (73 loc) · 2.26 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
70
71
72
73
74
/*
* line be like ax + by + c = 0
* -a/b = m;
*/
public class line extends shapes
{
point start,end;
double a,b,c;
boolean finiteLine;
line(point q1, point q2)
{ //throw exception if q1 and q2 are the same point.
if (q1.equals(q2)) {
throw new IllegalArgumentException("The start and end points of a line cannot be the same.");
}
start = q1;
end = q2;
//finiteLine = true;
b = start.x - end.x;
a = end.y - start.y;
c = -a * start.x - b * start.y;
if(a == 0){
start.y = -c/b;
end.y = -c/b;
start.x = 0;
end.x = 500;
}
else{
start.y = 0;
end.y = 500;
start.x = -1*(b*start.y + c)/a;
end.x = -1*(b*end.y + c)/a;
System.out.println("points are " + start.x + "," + start.y + " and " + end.x + "," + end.y);
}
}
line(double a, double b, double c)
{
// if a, b are set to 0 throw an exception
if (a == 0 && b == 0) {
throw new IllegalArgumentException("Coefficients 'a' and 'b' cannot both be zero.");
}
this.a = a;
this.b = b;
this.c = c;
finiteLine = false;
}
public double lineLenght()
{
return start.distanceTo(end);
}
public double getAngle() //returns angle between line and x-axis
{
return Math.atan2(a,b);
}
boolean intersects(circle c)
{
if(distanceToCircle(c) <= 0)
return true;
else return false;
}
double distanceToCircle(circle c)
{
System.out.println(Math.abs((this.a * c.center.x) + (this.b * c.center.y) + this.c) / Math.sqrt(a * a + b * b) - c.radius);
return Math.abs((this.a * c.center.x) + (this.b * c.center.y) + this.c) / Math.sqrt(a * a + b * b) - c.radius;
}
point IntersectionPoint(line l1)
{
point tmp = new point(0,0);
//include exception for division by 0 here
tmp.x = (this.b * l1.c - l1.b * this.c) / (this.a * l1.b - l1.a * this.b);
tmp.y = (l1.a * this.c - this.a - l1.c) / (this.a * l1.b - l1.a * this.b);
return tmp;
}
}