0

I searched for this and found similar answers but they were more math based than programming based, and I need a little help putting it in to code.

I have an arbitrary shape, but let's call it a diamond for now, with the following coordinates:

A = (0,0)

B = (50, 50)

C = (0, 100)

D = (-50, 50)

Think of the movement contraints on this as if you had a rubber band and moved a peg. The lines will always connect but their orientation to one another must not change. So since the slope of AB is 1, it needs to remain 1. I have that part working fine.

Now, say I move "B" such that it rests at (100, 100). I know the slopes of AB (unchanged), BC (before B was moved, and after, but after is useless and wrong), CD (unchanged) and DA (unchanged). I also know the coordinates of A, B and D. I need to find the position of C, such that all slopes remain unchanged and it rests at the logical point where BC and CD should intersect.

Mathematically, I know C belongs at (50, 150) now but I need a generalized formula to figure this out. This is a single example so what I'm looking for is something generalized enough to reuse. Of course, there are snags when lines are parallel or vertical but I can handle that.

This picture should help visualize it. B started at "B" and I moved it to "B2". C needs to move to ???

Thanks a ton if you can help!

Line constraints

EDIT: This seems to be the answer, but I'm anything but a math whiz and don't get how it translates in to some sort of generalized formula. How do you detect where two line segments intersect?

Community
  • 1
  • 1
oooyaya
  • 1,773
  • 1
  • 15
  • 39
  • 1
    I am not really understanding how you want this described without using math? It's a mathematical concept and even one that translates fairly simply into code. I'm not trying to be rude, just confused. – cjstehno Apr 26 '13 at 20:06
  • No no, of course it's math. I mean I found the algebraeic/geometric way to do it. I'm just having trouble putting it in to some generalized code. – oooyaya Apr 26 '13 at 20:07
  • Write out the steps you go through to solve the problem in math, create a function in JS that simulates it, and use IF/ELSE statements to give the code the logic necessary, i.e. which points it needs to recalculate. – Moses Apr 26 '13 at 20:09
  • When you move `B` to `B2` it's `x` and `y` coordinates increased by, say `sx` and `sy`. I suppose you only want to increrase the coordinates of `B2` by exact same numbers, right? – neo Apr 26 '13 at 20:10
  • @neo - you're right, but it needs to be more generic than that. Just taking the difference between B2 and B and applying that difference to C to find C2 would work, but not if the angles are more arbitrary. – oooyaya Apr 26 '13 at 20:12
  • I think you need to add more explanaition. (or even better: more examples) – neo Apr 26 '13 at 20:14
  • @oooyaya Why shouldn't that work? If B2 is moved 100 units up and 50 units right, C2 is 100 units up from C and 50 units right from C. – Ian Apr 26 '13 at 20:17
  • OK - I must not have communicated my problem very well. I'll reexplain but it will have to wait. Sorry to waste your time :( – oooyaya Apr 26 '13 at 20:29
  • @oooyaya Sorry, I didn't realize B was **moved to** B2. I thought another, new point was put down at B2 – Ian Apr 26 '13 at 20:31

0 Answers0