-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLine.java
More file actions
120 lines (106 loc) · 3.52 KB
/
Line.java
File metadata and controls
120 lines (106 loc) · 3.52 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
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
/**************************************************************/
/* Josh Lindoo
/* Login ID: lind6441
/* CS-203, Summer 2013
/* Programming Assignment 2
/* Line class: used to represent a line defined by two points
/* Requirements: uses point class
/**************************************************************/
public class Line {
//fields
//private Point point1;
//private Point point2;
private double x1; //x coord of first point
private double x2; //x coord of second point
private double y1; //y coord of first point
private double y2; //y coord of second point
//equation of this line
private double aCoef = 0;
private double bCoef = 0;
private double cCoef = 0;
//constructor using coordinates of two points
public Line(int x1, int y1, int x2, int y2) {
//store x and y values
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
//calculate line coefficients
aCoef = y2 - y1;
bCoef = x1 - x2;
bCoef = x1*y2 - y1*x2;
}
//constructor using Points as parameters
public Line(Point point1, Point point2) {
//store x and y values
this.x1 = point1.getX();
this.y1 = point1.getY();
this.x2 = point2.getX();
this.y2 = point2.getY();
//calculate line coefficients
aCoef = y2 - y1;
bCoef = x1 - x2;
bCoef = x1*y2 - y1*x2;
}
/**************************************************************/
/* Method: distFromLine
/* Purpose: Calculate the dist of a point from a line obj
/* Parameters:
/* Point testPoint -- point to calc dist to
/* Returns: double -- dist to the point
/**************************************************************/
public double distFromLine(Point testPoint) {
double x3 = testPoint.getX();
double y3 = testPoint.getY();
return (x1*y2) + (x3*y1) + (x2*y3) - (x3*y2) - (x2*y1) - (x1*y3);
}
/**************************************************************/
/* Method: isRight
/* Purpose: returns true if point is to the right of the line
/* Parameters:
/* Point testPoint -- point to test
/* Returns: boolean -- true if is to the right
/**************************************************************/
public boolean isRight(Point testPoint) {
if(distFromLine(testPoint) < 0) return true;
else return false;
}
/**************************************************************/
/* Method: getPoint1 and getPoint2
/* Purpose: Accessors for the two points that make up the line
/* Parameters:
/* none
/* Returns: Point -- the point requested
/**************************************************************/
public Point getPoint1() {
return new Point(x1,y1);
}
public Point getPoint2() {
return new Point(x2,y2);
}
/**************************************************************/
/* Method: compareTo
/* Purpose: compare point to line
/* Parameters:
/* Point point -- point to compare to line
/* Returns: void
/**************************************************************/
public int compareTo(Point point) {
//calculate and temporarily save sign to see where point falls
//on the graph
double sign = aCoef*point.getX() + bCoef*point.getY() - cCoef;
if(sign > 0) return 1; //point is on left
if(sign < 0) return -1; //point is on right
else return 0; //point is on this line
}
/**************************************************************/
/* Method: toString
/* Purpose: print str representation of line eqn
/* Parameters:
/* String[] args: filename or -debug filename
/* Returns: void
/**************************************************************/
public String toString() {
return aCoef + "x + " + bCoef + "y = " + cCoef;
}
}