immalee
immalee

Reputation: 43

Points and Segments in Java

I have created a Point class and a Segment class. I am trying to find a way to determine if a given point is part of the line segment, by using the absolute difference from the beginning point to the ending point of the segment. It has to be smaller than 0.01, so then the point Is part of my line. I have to do that using a contains method. I am giving you my code below. I am beginner. I know I have to do it using only one parameter in the contains method (Point p) but I can't do it right.

`public boolean contains(Point p){
    segment1x = currentPoint.pointX - point1.pointX;
    segment1y = currentPoint.pointY - point1.pointY;

    segment2x = point2.pointX - point1.pointX;
    segment2y = point2.pointY - point1.pointY;

    absoluteDifference = Math.abs((segment1x*segment2y)-(segment1y*segment2x));

    if(absoluteDifference !=0){
        return false;
    }
    return true;
}`

Upvotes: 0

Views: 2046

Answers (1)

bcr666
bcr666

Reputation: 2197

Determine the slope intercept equation of the line defined by the two segment points. Then determine if the point is on the line. Then determine if the point lies between the two segment points.

public class Segment {
    private Point point1;
    private Point point2;

    // the equation of a line = y=mx+b, this is called the slope intercept
    // m is the slope and b is the y-intercept (the point where x is 0 on the line)

    public Segment(Point point1, Point point2) {
        if (point1 == null || point2 == null) {
            throw new IllegalArgumentException("Cannot have null point(s).");
        }
        this.point1 = point1;
        this.point2 = point2;
    }

    public Point getPoint1() {
        return point1;
    }

    public Point getPoint2() {
        return point2;
    }

    public double getSlope() {
        double diffX = point2.getX() - point1.getX();
        double diffY = point2.getY() - point1.getY();
        return diffY / diffX;
    }

    public double getIntercept() {
        // if y=mx+b then b=y-mx
        return point1.getY() - getSlope() * point1.getX;
    }

    public boolean contains(Point p){
        double m = getSlope();
        double b = getIntercept();

        // now we use our slope intercept formula to find if the given point 
        // is on the line 
        double calculatedY = m * p.getX() + b;
        if (calculatedY == p.getY()) {
            // if we get here then the point is on the line
            // now we have to test if the point is between the segment ends
            double biggerX = Math.max(point1.getX(), point2.getX());
            double smallerX = Math.min(point1.getX(), point2.getX());
            double biggerY = Math.max(point1.getY(), point2.getY());
            double smallerY = Math.min(point1.getY(), point2.getY());
            if ((p.getX() >= smallerX && p.getX() <= biggerX)
                && (p.getY() >= smallerY && p.getY() <= biggerY)) {
                return true;
            }
            return false;
        }
        return false;
    } // end contains(Point p)
} // end class Segment

Upvotes: 0

Related Questions