Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
294 views
in Technique[技术] by (71.8m points)

geometry - Intersection of line and square in JAVA

JAVA:

I'm working on a grid where the centre point of each square is an integer co-ordinate and I'm drawing a line between two integers and need to check whether it goes through a specific grid square based around integer coordinates.

I've seen similar posts/google results but mine's a lot more basic so I'm wondering whether there's a simple solution I can't grasp. (Most probable)

Appreciate any help you can give!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think you are asking if one specific line intersects one specific square (if that problem is solved, it applies to any number of squares).

Try this. Say the line has equation y = a*x + b, and the lower left of the squares has coordinates (x1, y1) while the upper left corner has coordinates (x2, y2).

Find the points y' = a*x1 + b, y'' = a*x2 + b ("*" for multiply);

then depending on whether y' > y'' or vice versa, you have an intervals [y', y''] or [y'', y'], say for argument that the interval is [y', y''].

(In math notation, [m, n] stands for all numbers >= m, and <= n.)

If [y', y''] intersects [y1, y2], the line enters the square.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...