# Determine if line exists between two points

Page 1 of 1

## 4 Replies - 1332 Views - Last Post: 04 April 2012 - 11:54 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=273865&amp;s=34893a54c355e43855d1337323252424&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Zel2008

Reputation: 16
• Posts: 799
• Joined: 06-January 09

# Determine if line exists between two points

Posted 04 April 2012 - 10:40 AM

Hi everybody,

I'm trying to create a string representation of a panel. The panel has two points on it, A and B, and may or may not have a line drawn between those two points. If the line is not drawn, I'd like to save only the coordinates of A and B in a string. If the line is drawn, I'd like to save the coordinates of A and B, and then repeat them in the string as the endpoints of the line.

Essentially, to do this, all I need to know is if A and B are connected. This isn't trivial in Java 2D, as far as I can see, and I'm not looking for someone to do this for me, but I'd appreciate it if someone could give me a kickstart to get me started. I know how to find points A and B, it's figuring out the connection that I'm stuck on.

If anyone could give me some advice, I'd appreciate it.

Thanks,
Zel2008

Is This A Good Question/Topic? 0

## Replies To: Determine if line exists between two points

### #2 SwiftStriker00

• No idea why my code works

Reputation: 435
• Posts: 1,602
• Joined: 25-December 08

## Re: Determine if line exists between two points

Posted 04 April 2012 - 10:57 AM

Are you trying to just see if a line is drawn between the two points? If so

you would have to find componenets of the line, then pick a point on that line. So:

y=mx+b

we know m = delta Y / delta X
so m = (By - Ay) / ( Bx - Ax )

now we use a point, lets take A, and solve for B

Ay = m ( Ax ) + b
b = Ay / ( m * Ax )

now that we know m and B, we can pick some X where Ax < X < Bx.
Solve for that Y, and finally look and see if that point is painted on the line

### #3 Zel2008

Reputation: 16
• Posts: 799
• Joined: 06-January 09

## Re: Determine if line exists between two points

Posted 04 April 2012 - 11:08 AM

Thanks SwiftStriker,
That's exactly what I need, thank you. I guess I was focusing too much on Java 2D and ignoring my basic math. Thanks again!
Zel2008

### #4 blackcompe

• D.I.C Lover

Reputation: 1156
• Posts: 2,538
• Joined: 05-May 05

## Re: Determine if line exists between two points

Posted 04 April 2012 - 11:10 AM

Can you articulate? Or better yet, can you reduce your problem to something more rudimentary? You're mixing information about Java 2D, points, strings, and panels. Quite confusing. Whatever geometric problems you're trying to solve need not be complicated with talks of Swing and all that.

This post has been edited by blackcompe: 04 April 2012 - 11:11 AM

### #5 Zel2008

Reputation: 16
• Posts: 799
• Joined: 06-January 09

## Re: Determine if line exists between two points

Posted 04 April 2012 - 11:54 AM

Thanks blackcompe,
I'm fine now, I just needed to know if a line was drawn between two points. I'm still trying to figure out how to determine if a particular point is part of a painted line, but I'm sure I'll figure it out.
Thanks,
Zel2008