Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9250163
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:22:58+00:00 2026-06-18T10:22:58+00:00

Is there a class in the Java API that I can utilize to check

  • 0

Is there a class in the Java API that I can utilize to check if a line intersects a shape, given the starting and ending coordinates. I have tried playing around with Line2D objects, but the intersects() function for that class only seems to take in rectangular coordinates and Rectangle2D objects, which doesn’t suit my purposes as my shapes can have any number of sides.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T10:22:59+00:00Added an answer on June 18, 2026 at 10:22 am

    Like @Dan said you can check is your line intersects borders of your Polygon but you don’t have to check intersect with everyline created by any two points from Polygon, just have to check intersection of your line with every pair of two point like (p1,p2), (p2,p3), ... , (p(N-1), pN), (pN, p1). Polygon isn’t created from all combination of points in array but from line from p1 to p2 connected with line from p2 to p3 and so on till line conected from pN to p1.

    Some example here:

    Line2D myLine = getMyLine() // some imagine method giving your line
    Point2D[] polyPoints = getPolyPoints() // some imagine method giving point of your Polygon
    
    boolean intersect = false;
    for (int i = 0; i < polyPoints.length - 1; i++) {
       intersect = myLine.intersectsLine(polyPoints[i].getX(), polyPoints[i].getY(), polyPoints[i+1].getX(), polyPoints[i+1].getY());
       if (intersect) {
          break;
       }
    }
    if (!intersect) {
       intersect = myLine.intersectsLine(polyPoints[polyPoints.length - 1].getX(), polyPoints[polyPoints.length - 1].getY(), polyPoints[0].getX(), polyPoints[0].getY());
    }
    

    After execution of this code inside intersect you have value is your line intersects polygon or not.

    If you accept situation when Polygon can contains a line and this still means that line (as a path) intersect the polygon then create java Polygon instance from your points (vertex) and just invoke:

    if (!intersect) {
       intersect = myPolygon.contains(myLine.getP1()) || myPolygon.contains(myLine.getP2());
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'd like to know if there is any class in Java able to check,
My teacher told us that there is a default class in Java for binary
The java.io.Console class requires there to be a Console device (window). How can I
Is there an expandable array class in the Java API equivalent to the Vector
There are a variety of variables stored in the android.os.Build class that you can
Apparently there is a method that takes a char and returns a char: http://download.oracle.com/javase/6/docs/api/java/lang/Character.html#toLowerCase(char
In Java there is a nice library class java.util.Stack which implements push and pop
Is there a way for a Java class to have an awareness of its
Have there been incompatibilities between Java releases where Java source code/Java class files targeting
I Know there's a Vector class in java, but it seems to be just

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.