Reply to topic  [ 15 posts ] 
A little bit of maths help please? 
Author Message
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 7:35 pm
Posts: 6580
Location: Getting there
Reply with quote
Hi,

Just about to put pen to paper to work this out but thought I'd post here so I can check my answer when I finish.

OK, here's the problem...

I have 2 points (coords in 2D space). point A = (xa, ya), point B = (xb, yb) (read "xa" as "x subscript a" not "x * a" and so on...)

OK, simple enough so far.

Now I have a circle drawn around each point of radius 5.

I also have a line AB connecting the two points.

The problem is that I want the line to start and end at the edge of each circle but also at the points on each circle nearest each other. (If that makes sense).

Can anyone come up with a sensible way of calculating the adjusted points from A and from B? All I need is a dx and a dy value to add to one and subtract from the other but just trying to figure them out.

Thanks for any help.

_________________
Oliver Foggin - iPhone Dev

JJW009 wrote:
The count will go up until they stop counting. That's the way counting works.


Doodle Sub!
Game Of Life

Image Image


Mon Nov 28, 2011 8:23 pm
Profile WWW
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 7:35 pm
Posts: 6580
Location: Getting there
Reply with quote
Hmm... I think it was easier than I was thinking.

If the circles are radius r then...

the length of AB l = sqrt((xb - xa)^2 + (yb - ya)^2) (Pythagorus)

change in x from A to B = xb - xa
change in y from A to B = yb - ya

so...

the line will intersect the circle at x value = xa + (xb - xa) * r/l
and y value = ya + (yb - ya) * r /l

(assuming that xb >= xa and yb >= ya)

(then just have to subtract instead of add for the other end).

_________________
Oliver Foggin - iPhone Dev

JJW009 wrote:
The count will go up until they stop counting. That's the way counting works.


Doodle Sub!
Game Of Life

Image Image


Mon Nov 28, 2011 8:31 pm
Profile WWW
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 7:35 pm
Posts: 6580
Location: Getting there
Reply with quote
Almost!

It works in some cases and goes absolutely mental in others :D

_________________
Oliver Foggin - iPhone Dev

JJW009 wrote:
The count will go up until they stop counting. That's the way counting works.


Doodle Sub!
Game Of Life

Image Image


Mon Nov 28, 2011 8:45 pm
Profile WWW
I haven't seen my friends in so long
User avatar

Joined: Fri May 15, 2009 3:16 am
Posts: 6146
Location: Middle Earth
Reply with quote
Whilst you're on the subject of mental, have you considered that posting to yourself could be a sign that something is amiss? :P

_________________
Dive like a fish, drink like a fish!

><(((º>`•.¸¸.•´¯`•.¸><(((º>
•.¸¸.•´¯`•.¸><(((º>`•.¸¸.•´¯`•.¸><(((º>

If one is diving so close to the limits that +/- 1% will make a difference then the error has already been made.


Mon Nov 28, 2011 8:48 pm
Profile
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 7:35 pm
Posts: 6580
Location: Getting there
Reply with quote
belchingmatt wrote:
Whilst you're on the subject of mental, have you considered that posting to yourself could be a sign that something is amiss? :P

:lol:

Anyway, I was right but I'd put a - in the Pythagorus bit instead of a +.

For some reason it doesn't like calculating the square root of negative numbers :shock:

Stupid computers... they've got no imagination!

:lol:

_________________
Oliver Foggin - iPhone Dev

JJW009 wrote:
The count will go up until they stop counting. That's the way counting works.


Doodle Sub!
Game Of Life

Image Image


Mon Nov 28, 2011 9:00 pm
Profile WWW
What's a life?
User avatar

Joined: Fri Apr 24, 2009 10:21 am
Posts: 12700
Location: The Right Side of the Pennines (metaphorically & geographically)
Reply with quote
belchingmatt wrote:
Whilst you're on the subject of mental, have you considered that posting to yourself could be a sign that something is amiss? :P
:lol: :lol: :lol:

_________________
pcernie wrote:
'I'm going to snort this off your arse - for the benefit of government statistics, of course.'


Mon Nov 28, 2011 9:02 pm
Profile WWW
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 8:19 pm
Posts: 5071
Location: Manchester
Reply with quote
l3v1ck wrote:
belchingmatt wrote:
Whilst you're on the subject of mental, have you considered that posting to yourself could be a sign that something is amiss? :P
:lol: :lol: :lol:

I just looked for the "like" button under that post.


Mon Nov 28, 2011 9:20 pm
Profile
Spends far too much time on here
User avatar

Joined: Thu Apr 23, 2009 9:40 pm
Posts: 4876
Location: Newcastle
Reply with quote
Fogmeister wrote:
For some reason it doesn't like calculating the square root of negative numbers :shock:

Stupid computers... they've got no imagination!


As you are just calculating distance use the absolute method
Abs(-n) = n :P

_________________
Twitter
Charlie Brooker:
Macs are glorified Fisher-Price activity centres for adults; computers for scaredy cats too nervous to learn how proper computers work; computers for people who earnestly believe in feng shui.


Tue Nov 29, 2011 9:20 am
Profile
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 7:35 pm
Posts: 6580
Location: Getting there
Reply with quote
Fogmeister wrote:
For some reason it doesn't like calculating the square root of negative numbers :shock:

Stupid computers... they've got no imagination!

:lol:

:oops: no one got my joke :cry:

_________________
Oliver Foggin - iPhone Dev

JJW009 wrote:
The count will go up until they stop counting. That's the way counting works.


Doodle Sub!
Game Of Life

Image Image


Wed Nov 30, 2011 11:51 am
Profile WWW
I haven't seen my friends in so long
User avatar

Joined: Tue May 05, 2009 3:29 pm
Posts: 7173
Reply with quote
Fogmeister wrote:
:oops: no one got my joke :cry:

:cry:

_________________
timark_uk wrote:
That's your problem. You need Linux. That'll fix all your problems.
Mark


Wed Nov 30, 2011 11:53 am
Profile
Spends far too much time on here
User avatar

Joined: Thu Apr 23, 2009 9:40 pm
Posts: 4876
Location: Newcastle
Reply with quote
Fogmeister wrote:
:oops: no one got my joke :cry:


I got it.... it wasn't funny though :P

Did you hear about the constipated mathemitician? He worked it out with a pencil

_________________
Twitter
Charlie Brooker:
Macs are glorified Fisher-Price activity centres for adults; computers for scaredy cats too nervous to learn how proper computers work; computers for people who earnestly believe in feng shui.


Wed Nov 30, 2011 1:33 pm
Profile
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 7:35 pm
Posts: 6580
Location: Getting there
Reply with quote
finlay666 wrote:
Fogmeister wrote:
:oops: no one got my joke :cry:


I got it.... it wasn't funny though :P

Did you hear about the constipated mathemitician? He worked it out with a pencil

:lol:

Anyway, I've got another question and this time it's a bit harder. Tried (for about 5 mins) and gave up.

I've got a line now from point A to B but there are some lines that need to be drawn as double lines (i.e. like ======= but without the dashed pattern) if that makes sense.

So, I need to offset the start and end points and draw 2 lines.

i.e. if point A is (0, 0) and point B is (100, 0) then I draw one of the lines between the points... (0, 1) and (100, 1) and the other line between (0, -1) and (100, -1) to get 2 parallel lines.

However, I need to do it no matter what the coords are.

I really just need a single offset calculation and then need to add/subtract it from the start and end points.

Hope that makes sense and someone can help.

_________________
Oliver Foggin - iPhone Dev

JJW009 wrote:
The count will go up until they stop counting. That's the way counting works.


Doodle Sub!
Game Of Life

Image Image


Wed Nov 30, 2011 1:52 pm
Profile WWW
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 7:35 pm
Posts: 6580
Location: Getting there
Reply with quote
OK, done it again :D

It's actually really simple to calculate.

Line from (a,b) to (x,y).

Length l = sqrt((x-a)^2 + (y-b)^2)
Change = c

dx = (x - a) * c / l
dy = (y - b) * c / l

The shorter line (i.e. the one from the first question) is...
(a + dx, b + dy) to (x - dx, y - dy)

The parallel lines...

First one... (a - dy, b + dx) to (x - dy, y + dx)
Second one... (a + dy, b - dx) to (x + dy, y - dx)

That should work for all possible points.

_________________
Oliver Foggin - iPhone Dev

JJW009 wrote:
The count will go up until they stop counting. That's the way counting works.


Doodle Sub!
Game Of Life

Image Image


Wed Nov 30, 2011 2:19 pm
Profile WWW
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 7:35 pm
Posts: 6580
Location: Getting there
Reply with quote
Got home and it works perfectly :D

Also, because I realised I only have to work out the unit value (i.e. change of distance 1) of dx and dy once it has improved the performance of it :D

Thanks for the help :D

_________________
Oliver Foggin - iPhone Dev

JJW009 wrote:
The count will go up until they stop counting. That's the way counting works.


Doodle Sub!
Game Of Life

Image Image


Thu Dec 01, 2011 11:13 am
Profile WWW
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 8:19 pm
Posts: 5071
Location: Manchester
Reply with quote
Fogmeister wrote:
Thanks for the help :D

No problem mate, any time. :D


Thu Dec 01, 2011 12:39 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 15 posts ] 

Who is online

Users browsing this forum: No registered users and 33 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group
Designed by ST Software.