Author Topic: Zooming Splines - Get point of intersection of 2 straight lines  (Read 1985 times)

0 Members and 1 Guest are viewing this topic.

Offline mike_g

  • Amiga 1200
  • ****
  • Posts: 435
  • Karma: 34
    • View Profile
Hi, I'm having some problems zooming and rotating splines.

Basically there are two types of points thats easiest to show this pic:


Even numbered, red, points are for the start and end of a line.
Odd numbered, blue, points set the angle of the curve.

The way I am going about it is to first move all the red points (which I can do). Once the red points have been moved I run through the blue points. I need to place them at the location at which the light blue lines in the pic intersect. I'm a bit confused with the maths tho. Does anyone know how I should go about this?

Heres an example of my unfinished function in Blitz code:
Code: [Select]
Function ZoomSpline(s.spline, zoom)

For i=0 To s\size-1
cx = cx + s\pts[i]\x
cy = cy + s\pts[i]\y
Next
cx = cx / s\size
cy = cy / s\size

For i=0 To s\size-1  Step 2 ;line terminator points
dx# = s\pts[i]\x - cx
dy# = s\pts[i]\y - cy
angle# = ATan2(dy#, dx#)
s\pts[i]\x = s\pts[i]\x + Cos(angle#)*zoom
s\pts[i]\y = s\pts[i]\y + Sin(angle#)*zoom
Next

For i=1 To s\size-2 Step 2 ;angle points

Next
End Function
Cheers. 

Offline stormbringer

  • Time moves by fast, no second chance
  • Amiga 1200
  • ****
  • Posts: 453
  • Karma: 73
    • View Profile
    • www.retro-remakes.net
If you are using quadratic Bezier splines, you should just scale and rotate control points, that's it. then render your splince once you have rotated/moved/scaled your control points. Did you check this page: http://en.wikipedia.org/wiki/Bezier_curves ?

It has all the math you need. Let me know
We once had a passion
It all seemed so right
So young and so eager
No end in sight
But now we are prisoners
In our own hearts
Nothing seems real
It's all torn apart

Offline rain_storm

  • Here comes the Rain
  • DBF Aficionado
  • ******
  • Posts: 3088
  • Karma: 182
  • Rain never hurt nobody
    • View Profile
    • org_100h
This is the code I use for intersecting lines I dont know who the original author is but it works perfectly. though I think you should look into that page on the wiki cos this is for straight lines and not really for curves anyways it might be of use to you its written in yabasic but that shouldnt be a problem

Code: [Select]
sub seg2seg(px,py,nx,ny, x1,y1,x2,y2)
  local denom
  denom  = (y2-y1)*(nx-px) - (x2-x1)*(ny-py)
  if (denom = 0.0) return 0

  local nume_a
  nume_a = ((x2-x1)*(py-y1) - (y2-y1)*(px-x1)) / denom
  if (nume_a < 0.0) or (nume_a > 1.0) return 0

  local nume_b
  nume_b = ((nx-px)*(py-y1) - (ny-py)*(px-x1)) / denom
  if (nume_b < 0.0) or (nume_b > 1.0) return 0

  xi = px + nume_a*(nx-px)
  yi = py + nume_a*(ny-py)
  return 1
end sub

Challenge Trophies Won:

Offline stormbringer

  • Time moves by fast, no second chance
  • Amiga 1200
  • ****
  • Posts: 453
  • Karma: 73
    • View Profile
    • www.retro-remakes.net
it's very complex for curves, and really nobody does it. Except some industrial engineering programs for the automotive industry. What almost everybody does is approximate the curve with a poly-line and check the line intersection. You can improve the speed by checking the AA (axis aligned) box intersection of your little segments, but most of the time you do not even need that. Some good explanation of this here: http://local.wasp.uwa.edu.au/~pbourke/geometry/lineline2d/

However, either I did not understand what you want to do, or you are making things much more complicated than they are. Are you just drawing quadratic bezier splines? (that's what I see on your screenshot). If you are, then I do not understand your problem, really. The light blue point comes for free as you define it. Or are you drawing cubic bezier splines? in which case again, quadratic bezier splines can be easily converted to cubic bezier (and higher orders too of course).

Can you give some more information about what you are really doing there??
We once had a passion
It all seemed so right
So young and so eager
No end in sight
But now we are prisoners
In our own hearts
Nothing seems real
It's all torn apart

Offline mike_g

  • Amiga 1200
  • ****
  • Posts: 435
  • Karma: 34
    • View Profile
Hi, and thanks for the input, links and stuff. I have been playing around with this for a little bit but still no success.

Yeah they are just sequence of quadratic curves. I would have thought that it would be straight forward myself. TBH I don't know why just sacling all the points is not working for me. I'll have a play around with it a bit more and get back to you about it.

Cheers.

Offline stormbringer

  • Time moves by fast, no second chance
  • Amiga 1200
  • ****
  • Posts: 453
  • Karma: 73
    • View Profile
    • www.retro-remakes.net
just a question... you scale your points according to what? your code just multiplies coordinates by a scale factor, but I'd like to understand what you try to achieve. If you want your shape to be scaled by some factor, then the only thing you need to do, is scale the coordinates of your control points. In the case of quadratic bezier curves, that means scaling the start, bend and end points by some factor.

We once had a passion
It all seemed so right
So young and so eager
No end in sight
But now we are prisoners
In our own hearts
Nothing seems real
It's all torn apart

Offline mike_g

  • Amiga 1200
  • ****
  • Posts: 435
  • Karma: 34
    • View Profile
The code posted scaled from the centeriod, but I tried scaling it from the point of origin too (IE: top left of the screen). I dont know whats was wrong with my code, but I'll go over it again later on.

Offline mike_g

  • Amiga 1200
  • ****
  • Posts: 435
  • Karma: 34
    • View Profile
Well I just found out what my problem was. I was using ints where I needed floats >_<