Module org.dyn4j

Class Segment

    • Constructor Detail

      • Segment

        public Segment​(Vector2 point1,
                       Vector2 point2)
        Full constructor.

        Creates a new segment using the given points. The center will be the average of the points.

        A segment's points cannot be null or the same point.

        Parameters:
        point1 - the first point
        point2 - the second point
        Throws:
        NullPointerException - if point1 or point2 is null
        IllegalArgumentException - if point1 == point2
    • Method Detail

      • getVertices

        public Vector2[] getVertices()
        Description copied from interface: Wound
        Returns the array of vertices in local coordinates.

        For performance, this array may be the internal storage array of the shape. Both the array elements and their properties should not be modified via this method.

        It's possible that this method will be deprecated and/or removed in later versions.

        Specified by:
        getVertices in interface Wound
        Returns:
        Vector2[]
        See Also:
        Wound.getVertexIterator()
      • getNormals

        public Vector2[] getNormals()
        Description copied from interface: Wound
        Returns the array of edge normals in local coordinates.

        For performance, this array may be the internal storage array of the shape. Both the array elements and their properties should not be modified via this method.

        It's possible that this method will be deprecated and/or removed in later versions.

        Specified by:
        getNormals in interface Wound
        Returns:
        Vector2[]
        See Also:
        Wound.getNormalIterator()
      • getVertexIterator

        public Iterator<Vector2> getVertexIterator()
        Description copied from interface: Wound
        Returns an iterator for the vertices.

        The iterator does not support the remove method and will return a new Vector2 in the next method.

        This method is safer than the Wound.getVertices() since its not possible to modify the array or its elements.

        Specified by:
        getVertexIterator in interface Wound
        Returns:
        Iterator<Vector2>
      • getNormalIterator

        public Iterator<Vector2> getNormalIterator()
        Description copied from interface: Wound
        Returns an iterator for the normals.

        The iterator does not support the remove method and will return a new Vector2 in the next method rather than the underlying value.

        This method is safer than the Wound.getNormals() since its not possible to modify the array or its elements.

        Specified by:
        getNormalIterator in interface Wound
        Returns:
        Iterator<Vector2>
      • getRadius

        public double getRadius​(Vector2 center)
        Description copied from interface: Shape
        Returns the radius of the shape if the given point was the center for this shape.
        Specified by:
        getRadius in interface Shape
        Parameters:
        center - the center point
        Returns:
        double
      • getPoint1

        public Vector2 getPoint1()
        Returns point1 in local coordinates.
        Returns:
        Vector2
      • getPoint2

        public Vector2 getPoint2()
        Returns point2 in local coordinates.
        Returns:
        Vector2
      • getLength

        public double getLength()
        Returns the length of the line Segment.
        Returns:
        double
      • getLocation

        public static double getLocation​(Vector2 point,
                                         Vector2 linePoint1,
                                         Vector2 linePoint2)
        Determines where the point is relative to the given line.

        Set L = linePoint2 - linePoint1 Set P = point - linePoint1 location = L.cross(P)

        Returns 0 if the point lies on the line created from the line segment.
        Assuming a right handed coordinate system:
        Returns < 0 if the point lies on the right side of the line
        Returns > 0 if the point lies on the left side of the line

        Assumes all points are in world space.

        Parameters:
        point - the point
        linePoint1 - the first point of the line
        linePoint2 - the second point of the line
        Returns:
        double
        Throws:
        NullPointerException - if point, linePoint1, or linePoint2 is null
      • getPointOnLineClosestToPoint

        public static Vector2 getPointOnLineClosestToPoint​(Vector2 point,
                                                           Vector2 linePoint1,
                                                           Vector2 linePoint2)
        Returns the point on the given line closest to the given point.

        Project the point onto the line:

        Vline = P1 - P0 Vpoint = P0 - P Pclosest = Vpoint.project(Vline)

        Assumes all points are in world space.
        Parameters:
        point - the point
        linePoint1 - the first point of the line
        linePoint2 - the second point of the line
        Returns:
        Vector2
        Throws:
        NullPointerException - if point, linePoint1, or linePoint2 is null
        See Also:
        Vector2.project(Vector2)
      • getPointOnSegmentClosestToPoint

        public static Vector2 getPointOnSegmentClosestToPoint​(Vector2 point,
                                                              Vector2 linePoint1,
                                                              Vector2 linePoint2)
        Returns the point on the given line segment closest to the given point.

        If the point closest to the given point is on the line created by the given line segment, but is not on the line segment then either of the segments end points will be returned.

        Assumes all points are in world space.

        Parameters:
        point - the point
        linePoint1 - the first point of the line
        linePoint2 - the second point of the line
        Returns:
        Vector2
        Throws:
        NullPointerException - if point, linePoint1, or linePoint2 is null
        See Also:
        getPointOnLineClosestToPoint(Vector2, Vector2, Vector2)
      • getLineIntersection

        public static Vector2 getLineIntersection​(Vector2 ap1,
                                                  Vector2 ap2,
                                                  Vector2 bp1,
                                                  Vector2 bp2)
        Returns the intersection point of the two lines or null if they are parallel or coincident.

        If we let:

        A = Ap2 - Ap1 B = Bp2 - Bp1

        we can create two parametric equations:

        Q = Ap1 + taA Q = Bp1 + tbB

        Where Q is the intersection point:

        Ap1 + taA = Bp1 + tbB

        We can solve for tb by applying the cross product with A on both sides:

        (Ap1 + taA) x A = (Bp1 + tbB) x A Ap1 x A = Bp1 x A + tbB x A (Ap1 - Bp1) x A = tbB x A tb = ((Ap1 - Bp1) x A) / (B x A)

        If B x A == 0 then the lines are parallel. If both the top and bottom are zero then the lines are coincident.

        If the lines are parallel or coincident, null is returned.

        Parameters:
        ap1 - the first point of the first line
        ap2 - the second point of the first line
        bp1 - the first point of the second line
        bp2 - the second point of the second line
        Returns:
        Vector2 the intersection point; null if the lines are parallel or coincident
        Throws:
        NullPointerException - if ap1, ap2, bp1 or bp2 is null
        Since:
        3.1.1
        See Also:
        getSegmentIntersection(Vector2, Vector2, Vector2, Vector2)
      • getLineIntersection

        public Vector2 getLineIntersection​(Segment segment)
        Returns the line intersection of the given Segment and this Segment.

        This method treats this segment and the given segment as defining lines rather than segments.

        This method assumes that both this and the given segment are in the same space (either local or world space).

        If the lines are parallel or coincident, null is returned.

        Parameters:
        segment - the other segment
        Returns:
        Vector2
        Throws:
        NullPointerException - if the given segment is null
        Since:
        3.1.5
        See Also:
        getLineIntersection(Vector2, Vector2, Vector2, Vector2)
      • getSegmentIntersection

        public static Vector2 getSegmentIntersection​(Vector2 ap1,
                                                     Vector2 ap2,
                                                     Vector2 bp1,
                                                     Vector2 bp2)
        Returns the intersection point of the two line segments or null if they are parallel, coincident or don't intersect.

        If we let:

        A = Ap2 - Ap1 B = Bp2 - Bp1

        we can create two parametric equations:

        Q = Ap1 + taA Q = Bp1 + tbB

        Where Q is the intersection point:

        Ap1 + taA = Bp1 + tbB

        We can solve for tb by applying the cross product with A on both sides:

        (Ap1 + taA) x A = (Bp1 + tbB) x A Ap1 x A = Bp1 x A + tbB x A (Ap1 - Bp1) x A = tbB x A tb = ((Ap1 - Bp1) x A) / (B x A)

        If B x A == 0 then the segments are parallel. If the top == 0 then they don't intersect. If both the top and bottom are zero then the segments are coincident.

        If tb or ta less than zero or greater than 1 then the segments do not intersect.

        If the segments do not intersect, are parallel, or are coincident, null is returned.

        Parameters:
        ap1 - the first point of the first line segment
        ap2 - the second point of the first line segment
        bp1 - the first point of the second line segment
        bp2 - the second point of the second line segment
        Returns:
        Vector2 the intersection point; null if the line segments don't intersect, are parallel, or are coincident
        Throws:
        NullPointerException - if ap1, ap2, bp1, or bp2 is null
        Since:
        3.1.1
        See Also:
        getLineIntersection(Vector2, Vector2, Vector2, Vector2)
      • getFarthestFeature

        public static final EdgeFeature getFarthestFeature​(Vector2 v1,
                                                           Vector2 v2,
                                                           Vector2 vector,
                                                           Transform transform)
        Returns the farthest feature on the given segment.

        This will always return the segment itself, but must return it with the correct winding and the correct maximum.

        Parameters:
        v1 - the first segment vertex
        v2 - the second segment vertex
        vector - the direction
        transform - the local to world space Transform of this Convex Shape
        Returns:
        EdgeFeature
        Throws:
        NullPointerException - if v1, v2, vector, or transform is null
        Since:
        3.1.5
      • getFarthestPoint

        public static final Vector2 getFarthestPoint​(Vector2 v1,
                                                     Vector2 v2,
                                                     Vector2 vector,
                                                     Transform transform)
        Returns the farthest point on the given segment.
        Parameters:
        v1 - the first point of the segment
        v2 - the second point of the segment
        vector - the direction
        transform - the local to world space Transform of this Convex Shape
        Returns:
        Vector2
        Throws:
        NullPointerException - if v1, v2, vector, or transform is null
        Since:
        3.1.5
      • getAxes

        public Vector2[] getAxes​(Vector2[] foci,
                                 Transform transform)
        Description copied from interface: Convex
        Returns an array of separating axes to test for this Shape.

        The foci parameter is an array of circular focal points of the other Shape.

        If foci points are given, this method will return the separating axes for this Shape's voronoi regions also. The points in the foci array are assumed to be in world space.

        The returned axes are normalized and in world space.

        Specified by:
        getAxes in interface Convex
        Parameters:
        foci - the world space points representing foci of curved Shapes; can be null
        transform - the local to world space Transform of this Convex Shape
        Returns:
        Vector2[]
      • getFoci

        public Vector2[] getFoci​(Transform transform)
        Returns an array of world space foci points for circular curved edges.

        This method returns null if the Shape has zero curved edges.

        The returned points are in world space.

        Not applicable to this shape. Always returns null.

        Specified by:
        getFoci in interface Convex
        Parameters:
        transform - the local to world space Transform of this Convex Shape
        Returns:
        null
      • contains

        public boolean contains​(Vector2 point,
                                Transform transform)
        Returns true if the given point is inside this Shape.

        If the given point lies on an edge the point is considered to be inside the Shape.

        The given point is assumed to be in world space.

        Should almost always return false since this shape represents an infinitely thin line segment. Use the contains(Vector2, Transform, double) method instead for better, though technically inaccurate, results.

        Specified by:
        contains in interface Shape
        Parameters:
        point - world space point
        transform - Transform for this Shape
        Returns:
        boolean
      • contains

        public boolean contains​(Vector2 point,
                                Transform transform,
                                double radius)
        Returns true if the given point is inside this Shape.

        If the given point lies on an edge the point is considered to be inside the Shape.

        The given point is assumed to be in world space.

        If the radius is greater than zero then the point is tested to be within the shape expanded radially by the radius.

        Parameters:
        point - world space point
        transform - Transform for this Shape
        radius - the expansion radius; in the range [0, ∞]
        Returns:
        boolean
      • getFarthestPoint

        public Vector2 getFarthestPoint​(Vector2 vector,
                                        Transform transform)
        Description copied from interface: Convex
        Returns the point farthest in the direction of the given vector. If two points are equally distant along the given Vector2 the first one is used.

        The returned point is in world space.

        Specified by:
        getFarthestPoint in interface Convex
        Parameters:
        vector - the direction
        transform - the local to world space Transform of this Convex Shape
        Returns:
        Vector2
      • rotate

        public void rotate​(Rotation rotation,
                           double x,
                           double y)
        Description copied from interface: Rotatable
        Rotates the object about the given point.
        Specified by:
        rotate in interface Rotatable
        Overrides:
        rotate in class AbstractShape
        Parameters:
        rotation - the Rotation representing the rotation amount
        x - the x coordinate to rotate about
        y - the y coordinate to rotate about
      • translate

        public void translate​(double x,
                              double y)
        Description copied from interface: Translatable
        Translates the object the given amounts in the respective directions.
        Specified by:
        translate in interface Translatable
        Overrides:
        translate in class AbstractShape
        Parameters:
        x - the translation in the x direction
        y - the translation in the y direction
      • createMass

        public Mass createMass​(double density)
        Creates a Mass object using the geometric properties of this Segment and the given density.

        m = d * length I = l2 * m / 12

        Specified by:
        createMass in interface Shape
        Parameters:
        density - the density in kg/m2
        Returns:
        Mass the Mass of this Segment
      • createAABB

        public AABB createAABB​(Transform transform)
        Creates an AABB from this Shape after applying the given transformation to the shape.

        Be aware that this method could produce an infinitely thin AABB if this segment is aligned to either the x or y-axis.

        Specified by:
        createAABB in interface Shape
        Overrides:
        createAABB in class AbstractShape
        Parameters:
        transform - the Transform for this Shape
        Returns:
        AABB the AABB enclosing this Shape
      • computeAABB

        public void computeAABB​(Transform transform,
                                AABB aabb)
        Description copied from interface: Shape
        Computes the AABB from this Shape after applying the given transformation and places the result in the given AABB.
        Specified by:
        computeAABB in interface Shape
        Parameters:
        transform - the Transform for this Shape
        aabb - the destination AABB
      • getEdgeVector

        public Vector2 getEdgeVector()
        Returns a normalized edge vector for this segment pointing from the first vertex to the second.
        Returns:
        Vector2
        Since:
        4.2.0