The title says it all, Ive been searching around and couldnt find anything that was straight and to the point. How would I take a line with points (x1,y1) & (x2, y2) and check its intersection between a rectangle (xR,yR)? I saw in the Line2D package that there were some intersection methods but not sure how to set it all up. Can someone show me a correct way of setting it up to check for an intersection (collision)?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
Returns null if lines do not intersect. Modified some c code from another response to similar question to make it Java. Haven't bothered to look into how/why it works, but does the job I needed it to.
A rectangle is 4 lines. You could compute the intersect between your line and the 4 lines of the rectangle.
given the equations of two lines, they would intersect when x and y are equal.
y = m1x + b1 y = m2x + b2
solving the equation you should get:
x = b2 - b1 / (m1 - m2);
Note that if m1 == m2, the lines are parallel and will never intersect, watch out for the divided by 0 in this case.
Then, since you are dealing with segments ratter than infinite lines, check if the intersect falls off within your segments (check if both X and Y are within each segment's boundaries).
Using the available classes from the 2D Graphics API.
What this doesn't tell you, is where...