Author Topic: A Two-Roads Problem  (Read 11036 times)

0 Members and 1 Guest are viewing this topic.

This topic contains a post which is marked as Best Answer. Press here if you would like to see it.

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
A Two-Roads Problem
« on: January 31, 2020, 12:38:04 pm »
Hi folks. So I was pondering a statement made by OldMoses about bplus's work on tangents https://www.qb64.org/forum/index.php?topic=2132.0:

Quote
Nice. I think I'll file this one away as a possible basis for an orbital insertion routine.

So I decided to look at this. Along the way, I solved a toy problem that I think some of you would enjoy... as a challenge first if you aren't sick of them. I can imagine bplus and Steve looking at this a few different ways, I do wonder would OldMoses might come up with.

So the challenge is like this: Consider two curved roads or train tracks, modeled by parabolas to make it easy:

Code: QB64: [Select]
  1. FUNCTION ya (x)
  2.     ya = -x ^ 2 / 4 - 1
  3.  
  4. FUNCTION yb (x)
  5.     yb = x ^ 2 / 4 + 1

All that does is define two arc-shaped roads that don't intersect. So far so good?

Now consider the two points (-2,-2) and (1,1.25). Each of these points is on one of the roads. The job is to connect the two roads at the two points, but smoothly.

The code below produces the screenshot attached. I made a crude guess that seems to work for the first curve, but misses the target on the second curve. How do we connect the curves with a curvy line that joins each road smoothly?

Code: QB64: [Select]
  1.  
  2. CALL cline(0, 0, _WIDTH / 2, 0, 7)
  3. CALL cline(0, 0, -_WIDTH / 2, 0, 7)
  4. CALL cline(0, 0, 0, _HEIGHT / 2, 7)
  5. CALL cline(0, 0, 0, -_HEIGHT / 2, 7)
  6.  
  7. zoom = 75
  8. CALL ccircle(-2 * zoom, ya(-2) * zoom, 10, 14)
  9. CALL ccircle(1 * zoom, yb(1) * zoom, 10, 15)
  10. FOR x = -2.5 TO 2 STEP .01
  11.     CALL ccircle(x * zoom, ya(x) * zoom, 1, 14)
  12.     CALL ccircle(x * zoom, yb(x) * zoom, 1, 15)
  13.     CALL ccircle(x * zoom, yc(x) * zoom, 1, 5)
  14.  
  15.  
  16. FUNCTION ya (x)
  17.     ya = -x ^ 2 / 4 - 1
  18.  
  19. FUNCTION yb (x)
  20.     yb = x ^ 2 / 4 + 1
  21.  
  22. FUNCTION yc (x)
  23.     yc = x ' What should this be in order to smoothly join each curve?
  24.  
  25. SUB cline (x1, y1, x2, y2, col)
  26.     LINE (_WIDTH / 2 + x1, -y1 + _HEIGHT / 2)-(_WIDTH / 2 + x2, -y2 + _HEIGHT / 2), col
  27.  
  28. SUB ccircle (x1, y1, r, col)
  29.     CIRCLE (_WIDTH / 2 + x1, -y1 + _HEIGHT / 2), r, col
  30.  

roads.png
* roads.png (Filesize: 6.55 KB, Dimensions: 640x505, Views: 294)
« Last Edit: January 31, 2020, 01:13:39 pm by STxAxTIC »
You're not done when it works, you're done when it's right.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: A Two-Roads Problem
« Reply #1 on: January 31, 2020, 12:52:56 pm »
Well I think I've solved the problem of what a "cured" road is, it is one without a v in it. ;-))

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: A Two-Roads Problem
« Reply #2 on: January 31, 2020, 12:55:08 pm »
(Damn you shoulda quoted cause I fixed that right away.)
You're not done when it works, you're done when it's right.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: A Two-Roads Problem
« Reply #3 on: January 31, 2020, 01:20:35 pm »
Here’s an idea:   Draw a straight line from the top circle, and a straight line from the bottom circle.  Then choose a midpoint between those lines and create a circle. 

To illustrate: In your picture above, you’d have what looks like the end of a paperclip slid between the two points, with the “U” being off to the left side of the two arcs.

Would such a solution as the above be considered valid for smoothly connecting the two points?  It seems like a very simple solution to the problem at hand.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: A Two-Roads Problem
« Reply #4 on: January 31, 2020, 03:28:03 pm »
Nice thinking Steve, but theres a problem with that proposed curve in that it isnt smooth. In a car driving analogy, note that the steering wheel would have to jerk *instantly* when you transition from the line to the circle. A real road/car would forbid an instant jump like that, so you need to enter/exit the circle more gradually than a brutal tangent line. This is especially evident for smaller circles but true for all circles. That doesnt mean a part of the solution cant be line-like or circle-like though.

Said another way, you want a curve to join at the points, yes. You also want the slope of the curve to match the slope of the road where it joins, check. Here's the subtle part: the slope of the slope of the curve must also match the slope of the slope of the road at the intersection points. This guarantees a jerk-free transition.
« Last Edit: January 31, 2020, 03:30:38 pm by STxAxTIC »
You're not done when it works, you're done when it's right.

Offline OldMoses

  • Seasoned Forum Regular
  • Posts: 469
    • View Profile
Re: A Two-Roads Problem
« Reply #5 on: January 31, 2020, 04:43:15 pm »
OMG, did I start something I can't finish?

I'm thinking something along the lines of a cube function, but getting it to fit is a bit beyond my pay grade.

Code: QB64: [Select]
  1. A& = _NEWIMAGE(650, 650, 32)
  2. _TITLE "Graph"
  3.  
  4. WINDOW (-15, 15)-(15, -15)
  5. g = -16
  6.     g = g + 1
  7.     LINE (-15, g)-(15, g), &H7F7F7F7F '                         horizontal grid lines
  8.     LINE (g, -15)-(g, 15), &H7F7F7F7F '
  9. LOOP UNTIL g = 15
  10. LINE (0, -15)-(0, 15), &HFF0000FF, BF
  11. LINE (-15, 0)-(15, 0), &HFF0000FF, BF
  12. FOR x = -15 TO 15 STEP .01
  13.     y = (.5 * x) ^ 3
  14.     PSET (x, y)
  15.  
  16.  
« Last Edit: January 31, 2020, 05:11:38 pm by OldMoses »

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: A Two-Roads Problem
« Reply #6 on: February 01, 2020, 12:01:01 am »
I'll post the answer as a hint, but this is by no means a solution.

Code: QB64: [Select]
  1.  
  2. CALL cline(0, 0, _WIDTH / 2, 0, 7)
  3. CALL cline(0, 0, -_WIDTH / 2, 0, 7)
  4. CALL cline(0, 0, 0, _HEIGHT / 2, 7)
  5. CALL cline(0, 0, 0, -_HEIGHT / 2, 7)
  6.  
  7. zoom = 75
  8. CALL ccircle(-2 * zoom, ya(-2) * zoom, 10, 14)
  9. CALL ccircle(1 * zoom, yb(1) * zoom, 10, 15)
  10. FOR x = -2.5 TO 2 STEP .01
  11.     CALL ccircle(x * zoom, ya(x) * zoom, 1, 14)
  12.     CALL ccircle(x * zoom, yb(x) * zoom, 1, 15)
  13.     CALL ccircle(x * zoom, yc(x) * zoom, 1, 5)
  14.  
  15.  
  16. FUNCTION ya (x)
  17.     ya = -x ^ 2 / 4 - 1
  18.  
  19. FUNCTION yb (x)
  20.     yb = x ^ 2 / 4 + 1
  21.  
  22. FUNCTION yc (x)
  23.     yc = 0.5123456790123457 + 1.1419753086419753 * x - 0.40895061728395077 * x ^ 2 - 0.12345679012345677 * x ^ 3 + 0.09413580246913578 * x ^ 4 + 0.03395061728395061 * x ^ 5
  24.  
  25. SUB cline (x1, y1, x2, y2, col)
  26.     LINE (_WIDTH / 2 + x1, -y1 + _HEIGHT / 2)-(_WIDTH / 2 + x2, -y2 + _HEIGHT / 2), col
  27.  
  28. SUB ccircle (x1, y1, r, col)
  29.     CIRCLE (_WIDTH / 2 + x1, -y1 + _HEIGHT / 2), r, col
You're not done when it works, you're done when it's right.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: A Two-Roads Problem
« Reply #7 on: February 02, 2020, 01:05:35 pm »
Code: [Select]
SCREEN _NEWIMAGE(640, 480, 32)
WINDOW (-3.2, -2.4)-(3.2, 2.4)
FOR x = -3.2 TO 3.2 STEP .01 'The two arcs
    PSET (x, ya(x)), -1
    PSET (x, yb(x)), -1
NEXT

LowPoint = 2.4: HighPoint = -2.4
FOR y = 2.4 TO 0 STEP -0.01
    FOR x = -3.2 TO 3.2 STEP 0.01
        IF POINT(x, y) <> 0 AND y < LowPoint THEN LowPoint = y
    NEXT
NEXT
FOR y = -2.4 TO 0 STEP 0.01
    FOR x = -3.2 TO 3.2 STEP 0.01
        IF POINT(x, y) <> 0 AND y > HighPoint THEN HighPoint = y
    NEXT
NEXT

Midpoint = (HighPoint - LowPoint) / 2

CIRCLE (0, LowPoint + Midpoint / 2), Midpoint / 2
CIRCLE (0, HighPoint - Midpoint / 2), Midpoint / 2






CIRCLE (-2, -2), .1, &HFFFF0000 'one point
CIRCLE (1, 1.25), .1, &HFFFF0000 'second point

SLEEP

FUNCTION ya (x)
    ya = -x ^ 2 / 4 - 1
END FUNCTION

FUNCTION yb (x)
    yb = x ^ 2 / 4 + 1
END FUNCTION

Start at the bottom left, follow the arc to your first point and board the train to the apex and get on the circle.  Continue to the next circle and get on it.  Exit the circle at the apex of the top arc and continue on to your destination at the second point.
Two Roads.jpg
* Two Roads.jpg (Filesize: 18.28 KB, Dimensions: 646x509, Views: 311)
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline OldMoses

  • Seasoned Forum Regular
  • Posts: 469
    • View Profile
Re: A Two-Roads Problem
« Reply #8 on: February 02, 2020, 01:12:32 pm »
Bravo! That's what I call thinking out of the box.

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: A Two-Roads Problem
« Reply #9 on: February 02, 2020, 01:13:32 pm »
*facepalm*

Not because it's wrong (you are supposed to intersect at the points), but I explained this more than once (not shown here). You could have described this without the circles ya know... Isn't that almost what you did above?

Make sure your method can start anywhere on curve one, and end anywhere on curve 2.

OldMoses: You really want your orbital simulation doing stuff like that? Lol
« Last Edit: February 02, 2020, 01:23:53 pm by STxAxTIC »
You're not done when it works, you're done when it's right.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: A Two-Roads Problem
« Reply #10 on: February 02, 2020, 01:26:06 pm »
*facepalm*

Not because it's wrong (you are supposed to intersect at the points), but I explained this more than once (not shown here). You could have described this without the circles ya know... Isn't that almost what you did above?

Make sure your method can start anywhere on curve one, and end anywhere on curve 2.

Why wouldn't it work with any spot on curve one and end anywhere on curve 2?  Unless you want to go from the same side to the same side?  In that case, you just build a third circle to connect the other two and you use it as a direction changer.
Two Roads Anywhere.jpg
* Two Roads Anywhere.jpg (Filesize: 18.83 KB, Dimensions: 646x509, Views: 320)
« Last Edit: February 02, 2020, 01:32:37 pm by SMcNeill »
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: A Two-Roads Problem
« Reply #11 on: February 02, 2020, 01:34:56 pm »
I see the construction you're going for - not to shit on it. But I kinda got rid of circles already if the point wasn't too subtle. I will try to reiterate why your path still isn't smooth.

The instantaneous curvature of the curve your're on is equal to one divided the radius of an imaginary circle (somewhere in space) whose edge instantaneously coincides with your curve. Straight lines have zero curvature, as a circle of infinite radius parked very far away would be needed to match its edge to a straight line.

Okay. So when you jump from a straight line to a curve, you *instantly* change curvature from 0 to 1/r. Can't have instant jumps. It gets even worse when you go from one circle to another circle that bends the other way, because your curvature jump is 1/r1 + 1/r2. We need a path that is truly smooth.

Hint: The actual answer is above. See if you can get close.
« Last Edit: February 02, 2020, 01:43:25 pm by STxAxTIC »
You're not done when it works, you're done when it's right.

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: A Two-Roads Problem
« Reply #12 on: February 02, 2020, 01:58:57 pm »
Here's the same problem with less unusable road:
roads2.png
* roads2.png (Filesize: 3.54 KB, Dimensions: 638x480, Views: 285)
You're not done when it works, you're done when it's right.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: A Two-Roads Problem
« Reply #13 on: February 02, 2020, 02:02:29 pm »
Here's the same problem with less unusable road:

And here, we just use our warp drive, fold dimensional space in two and stick a pencil in it, and go instantly from point A to point B at whatever heading, direction, and speed which we want to arrive at...  :P
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: A Two-Roads Problem
« Reply #14 on: February 02, 2020, 02:21:39 pm »
Okay DarkStar :-)
You're not done when it works, you're done when it's right.