Package drawit
Class IntPoint
java.lang.Object
drawit.IntPoint
public class IntPoint extends Object
An immutable abstraction for a point in the two-dimensional plane with
int
coordinates.- Immutable
- Any two calls of any zero-argument getter of this class on the same target object return equal values. (A getter is a method whose name starts with "get" or "is" followed by a capital letter.)
-
Constructor Summary
Constructors Constructor Description IntPoint(int x, int y)
Initializes this point with the given coordinates. -
Method Summary
Modifier and Type Method Description DoublePoint
asDoublePoint()
Returns aDoublePoint
object that represents the same 2D point represented by thisIntPoint
object.boolean
equals(IntPoint other)
Returnstrue
if this point has the same coordinates as the given point; returnsfalse
otherwise.int
getX()
Returns this point's X coordinate.int
getY()
Returns this point's Y coordinate.boolean
isOnLineSegment(IntPoint b, IntPoint c)
Returns true iff this point is on open line segmentbc
.static boolean
lineSegmentsIntersect(IntPoint a, IntPoint b, IntPoint c, IntPoint d)
Returns true iff the open line segmentab
intersects the open line segmentcd
.IntVector
minus(IntPoint other)
Returns anIntVector
object representing the displacement fromother
tothis
.IntPoint
plus(IntVector vector)
Returns anIntPoint
object representing the point obtained by displacing this point by the given vector.
-
Constructor Details
-
IntPoint
public IntPoint(int x, int y)Initializes this point with the given coordinates.- Mutates:
this
- Postconditions:
getX() == x
getY() == y
-
-
Method Details
-
getX
public int getX()Returns this point's X coordinate. -
getY
public int getY()Returns this point's Y coordinate. -
equals
Returnstrue
if this point has the same coordinates as the given point; returnsfalse
otherwise.- Preconditions:
other != null
- Postconditions:
result == (this.getX() == other.getX() && this.getY() == other.getY())
-
minus
Returns anIntVector
object representing the displacement fromother
tothis
.- Preconditions:
other != null
- Postconditions:
result != null
result.getX() == this.getX() - other.getX()
result.getY() == this.getY() - other.getY()
-
isOnLineSegment
Returns true iff this point is on open line segmentbc
. An open line segment does not include its endpoints.Implementation hints: Call this point
a
. First check ifba
is collinear withbc
. If not, returnfalse
. Then check that the dot product ofba
andbc
is between zero and the dot product ofbc
andbc
.- Preconditions:
b != null
0 <= b.getX() && b.getX() <= 10000 && 0 <= b.getY() && b.getY() <= 10000
c != null
0 <= c.getX() && c.getX() <= 10000 && 0 <= c.getY() && c.getY() <= 10000
- Postconditions:
result == ( this.minus(b).isCollinearWith(c.minus(b)) && 0 < this.minus(b).dotProduct(c.minus(b)) && this.minus(b).dotProduct(c.minus(b)) < c.minus(b).dotProduct(c.minus(b)) )
-
asDoublePoint
Returns aDoublePoint
object that represents the same 2D point represented by thisIntPoint
object.- Postconditions:
result != null
result.getX() == this.getX()
result.getY() == this.getY()
-
plus
Returns anIntPoint
object representing the point obtained by displacing this point by the given vector.- Preconditions:
vector != null
- Postconditions:
result != null
result.getX() == this.getX() + vector.getX()
result.getY() == this.getY() + vector.getY()
-
lineSegmentsIntersect
Returns true iff the open line segmentab
intersects the open line segmentcd
.Implementation Hints: Assume the precondition holds. Then
ab
intersectscd
if and only ifab
straddles the carrier ofcd
andcd
straddles the carrier ofab
. Two points straddle a line if they are on opposite sides of the line.Specifically,
cd
straddles the carrier ofab
iff (the signum of the cross product ofac
andab
) times (the signum of the cross product ofad
andab
) is negative.The signum of a number
x
is -1 ifx
is negative, 0 ifx
is zero, and1
otherwise. SeeMath.signum(double)
.- Preconditions:
a != null
b != null
c != null
d != null
- The line segments have at most one point in common.
-