Results 1 to 25 of 25

Thread: Interpolation of lines in polygon system

  1. #1

    Thread Starter
    Frenzied Member cyborg's Avatar
    Join Date
    May 2000
    Location
    Sweden
    Posts
    1,755

    Interpolation of lines in polygon system

    Last edited by cyborg; Oct 6th, 2002 at 08:22 PM.
    Check out the FAQ and do a search before you post.
    My tutorials: Anti-Alias Pixels, Accurate Game Loop, Resource File

  2. #2
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Question

    When, in your drawing, you say "clicking the polygon", do you really mean "clicking the polygon's vertex"?

  3. #3
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573
    Well, I suppose the answer to my previous post is yes.

    Referring to the attached drawing (I have only worked out the coordinates for one of the blue points, the other one is calculated in an analogous fashion):

    alpha=0.5*(phi1+phi2)

    phi1=arctan[(y(i)-y(i-1))/(x(i)-x(i-1))]
    phi2=arctan[(y(i+1)-y(i))/(x(i+1)-x(i))]

    beta=0.5*(theta1+theta2)

    theta1=arctan[(y(i+2)-y(i+1))/(x(i+2)-x(i+1))]
    theta2=-phi2

    Equation of straight line BP(i): can be calculated knowing that the slope must be tan(alpha) and that it must pass through point
    P(i). The result is:

    y1=(x-x(i))tan(phi1)+y(i)

    By a similar calculation, line AP(i+1) is:

    y2=(x-x(i+1))tan(phi2)+y(i+1)

    The blue point (O in the drawing) lies at the intersection of these 2 lines. To determine its coordinates, set y1=y2 and solve for x:

    (x-x(i))tan(phi1)+y(i)=(x-x(i+1))*tan(phi2)+y(i+1)

    so that

    xo=(y(i+1)-y(i)+x(i)tan(phi1)-x(i+1)tan(phi2))/(tan(phi1)-tan(phi2))

    and substituting xo into one of the above straight line equations,

    yo=(xo-x(i))tan(phi1)+y(i)
    Attached Images Attached Images  

  4. #4

    Thread Starter
    Frenzied Member cyborg's Avatar
    Join Date
    May 2000
    Location
    Sweden
    Posts
    1,755
    well actually i meant that the user was clicking the polygon itself to make one new dot at each side of it, but clicking the vertex would also do fine!

    the problem is that im not fully educated in maths so i would be very grateful if you could show me some actual VB code, because it would be easier for me to understand!

    Thanks for your time!
    Check out the FAQ and do a search before you post.
    My tutorials: Anti-Alias Pixels, Accurate Game Loop, Resource File

  5. #5

    Thread Starter
    Frenzied Member cyborg's Avatar
    Join Date
    May 2000
    Location
    Sweden
    Posts
    1,755
    i forgot to mention...
    is there a way to make the new polygon(s) to shape the vertex more like a circle?
    that would be much better!
    Check out the FAQ and do a search before you post.
    My tutorials: Anti-Alias Pixels, Accurate Game Loop, Resource File

  6. #6
    So Unbanned DiGiTaIErRoR's Avatar
    Join Date
    Apr 1999
    Location
    /dev/null
    Posts
    4,111
    You could get the points of a circle to n points.

  7. #7

    Thread Starter
    Frenzied Member cyborg's Avatar
    Join Date
    May 2000
    Location
    Sweden
    Posts
    1,755
    I mean a more smooth shape....not really circle shaped, but it doesnt have to be exactly half the angle, if there is a better way to do it...

    as i said: im not fully educated in math so its easier for me to understand VB code...

    If you could do some of the code i could maybe do some other...
    Check out the FAQ and do a search before you post.
    My tutorials: Anti-Alias Pixels, Accurate Game Loop, Resource File

  8. #8
    So Unbanned DiGiTaIErRoR's Avatar
    Join Date
    Apr 1999
    Location
    /dev/null
    Posts
    4,111
    So then you should make each section 2.

    Points along a curve.

  9. #9

    Thread Starter
    Frenzied Member cyborg's Avatar
    Join Date
    May 2000
    Location
    Sweden
    Posts
    1,755
    but how do i know the curve?

    ive made (and attached) a little illustration...

    compare the two different lines, then you'll see what i want

    i want the program to create new polygons to smoothen the curves.
    Attached Images Attached Images  
    Check out the FAQ and do a search before you post.
    My tutorials: Anti-Alias Pixels, Accurate Game Loop, Resource File

  10. #10
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Lightbulb

    Have you tried "spline" interpolation? In some instances it produces a very smooth nice-looking curve.
    I don't have vb code for it but maybe you can make do with a Fortran subroutine. It's similar enough that you could easily "translate". Are you interested?

  11. #11
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573
    You know, after all I've found the vb version of the spline routines. I'm not sure it can solve your problem as it is perhaps more suited to some less-smooth type of data, I mean, maybe it will show some sine/cosine-like nasty waves. If you want to give it a try, use the Spline sub to calculate your coefficients and then SplineEval to calculate the polynomial fit for a single point each time it is called.

    VB Code:
    1. Sub Spline(n, x, y, b, c, d)
    2.     Dim t As Single
    3.     Dim ib As Integer, nmi As Integer
    4. '-----------------------------------------------------------------------------------
    5. 'This subroutine calculates the corfficients b(i),c(i),d(i)
    6. 'with i=1,...,n of the n cubic spline polynomes for the n
    7. 'points x(i),y(i), (i=1,...,n)
    8. 'In the calling program, add these 2 lines:
    9. 'dim x(1 to n) as single,y(1 to n) as single
    10. 'dim b(1 to n) as single,c(1 to n) as single,d(1 to n) as single
    11. '-----------------------------------------------------------------------------------
    12.     If n < 2 Then
    13.     'Too few points
    14.     'Add some error code here
    15.         Exit Sub
    16.     End If
    17.     If n < 3 Then
    18.         b(1) = (y(2) - y(1)) / (x(2) - x(1))
    19.         c(1) = 0
    20.         d(1) = 0
    21.         b(2) = b(1)
    22.         c(2) = 0
    23.         d(2) = 0
    24.         Exit Sub
    25.     End If
    26.     nmi = n - 1
    27.     d(1) = x(2) - x(1)
    28.     c(2) = (x(2) - x(1)) / d(1)
    29.     For i = 2 To nmi
    30.         d(i) = x(i + 1) - x(i)
    31.         b(i) = 2 * (d(i - 1) + d(i))
    32.         c(i + 1) = (y(i + 1) - y(i)) / d(i)
    33.         c(i) = c(i + 1) - c(i)
    34.     Next
    35.     b(1) = -d(1)
    36.     b(n) = -d(n - 1)
    37.     c(1) = 0
    38.     c(n) = 0
    39.     If n <> 3 Then
    40.         c(1) = c(3) / (x(4) - x(2)) - c(2) / (x(3) - x(1))
    41.         c(n) = c(n - 1) / (x(n) - x(n - 2)) - c(n - 2) / (x(n - 1) - x(n - 3))
    42.         c(1) = c(1) * d(1) ^ 2 / (x(4) - x(1))
    43.         c(n) = -c(n) * d(n - 1) ^ 2 / (x(n) - x(n - 3))
    44.     End If
    45.     For i = 2 To n
    46.         t = d(i - 1) / b(i - 1)
    47.         b(i) = b(i) - t * d(i - 1)
    48.         c(i) = c(i) - t * c(i - 1)
    49.     Next
    50.     c(n) = c(n) / b(n)
    51.     For ib = 1 To nmi
    52.         i = n - ib
    53.         c(i) = (c(i) - d(i) * c(i + 1)) / b(i)
    54.     Next
    55.     b(n) = (y(n) - y(nmi)) / d(nmi) + d(nmi) * (c(nmi) + 2 * c(n))
    56.     For i = 1 To nmi
    57.         b(i) = (y(i + 1) - y(i)) / d(i) - d(i) * (c(i + 1) + 2 * c(i))
    58.         d(i) = (c(i + 1) - c(i)) / d(i)
    59.         c(i) = 3 * c(i)
    60.     Next
    61.     c(n) = 3 * c(n)
    62.     d(n) = d(n - 1)
    63. End Sub
    64. '-----------------------------------------------------------------------------------
    65. Function SplineEval(n, u, x, y, b, c, d)
    66.     Dim s As Single
    67.     Dim j As Integer, k As Integer
    68. 'Given points x(i),y(i),(i=1,...,n), the coefficients b(i),c(i) and d(i),
    69. '(i=1,...,n) of the corresponding cubic spline polynomes and a
    70. 'value x=u, this function determines the interval [x(i),x(i+1)] in
    71. 'which u is contained and calculates P(u), P being the polynome
    72. 'corresponding to that interval
    73.     i = 1
    74.     If i >= n Then i = 1
    75.     If u < x(i) Then GoTo First
    76.     If u <= x(i + 1) Then GoTo Third
    77. First:
    78. 'Binary search
    79.     i = 1
    80.     j = n + 1
    81. Second:
    82.     k = (i + j) \ 2
    83.     If u < x(k) Then j = k
    84.     If u >= x(k) Then i = k
    85.     If j > i + 1 Then GoTo Second
    86. Third:
    87. 'Evaluate spline
    88.     s = u - x(i)
    89.     SplineEval = y(i) + s * (b(i) + s * (c(i) + s * d(i)))
    90. End Function
    Last edited by krtxmrtz; Dec 12th, 2002 at 03:41 AM.

  12. #12
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Lightbulb

    Yet another suggestion: I seem to recall that for polygon data smoothing a good tool is the so-called Bezier's polynomes, but I have never worked with them.

    I think you should try a google or altavista search for "Bezier lines" or "Bezier curves", or go directly to this site: http://www.moshplant.com/direct-or/bezier/index.html

    Good luck!

  13. #13

    Thread Starter
    Frenzied Member cyborg's Avatar
    Join Date
    May 2000
    Location
    Sweden
    Posts
    1,755
    ok...
    i'll try those things!
    thanks for all your help!
    Check out the FAQ and do a search before you post.
    My tutorials: Anti-Alias Pixels, Accurate Game Loop, Resource File

  14. #14

    Thread Starter
    Frenzied Member cyborg's Avatar
    Join Date
    May 2000
    Location
    Sweden
    Posts
    1,755
    hey!
    i found out a way to do it the first way i suggested, but somethings a bit wrong...

    ill attach the source for you to see whats wrong...
    pretty bad writted code, but you'll probably understand it.
    Attached Files Attached Files
    Check out the FAQ and do a search before you post.
    My tutorials: Anti-Alias Pixels, Accurate Game Loop, Resource File

  15. #15

    Thread Starter
    Frenzied Member cyborg's Avatar
    Join Date
    May 2000
    Location
    Sweden
    Posts
    1,755
    btw. i tried the bezier curve and it was awesome!
    Check out the FAQ and do a search before you post.
    My tutorials: Anti-Alias Pixels, Accurate Game Loop, Resource File

  16. #16

    Thread Starter
    Frenzied Member cyborg's Avatar
    Join Date
    May 2000
    Location
    Sweden
    Posts
    1,755
    check out this nice app i made with bezier curves!
    Attached Files Attached Files
    Check out the FAQ and do a search before you post.
    My tutorials: Anti-Alias Pixels, Accurate Game Loop, Resource File

  17. #17

    Thread Starter
    Frenzied Member cyborg's Avatar
    Join Date
    May 2000
    Location
    Sweden
    Posts
    1,755
    newer version!
    Attached Files Attached Files
    Check out the FAQ and do a search before you post.
    My tutorials: Anti-Alias Pixels, Accurate Game Loop, Resource File

  18. #18
    Good Ol' Platypus Sastraxi's Avatar
    Join Date
    Jan 2000
    Location
    Ontario, Canada
    Posts
    5,134
    Good work
    All contents of the above post that aren't somebody elses are mine, not the property of some media corporation.
    (Just a heads-up)

  19. #19

    Thread Starter
    Frenzied Member cyborg's Avatar
    Join Date
    May 2000
    Location
    Sweden
    Posts
    1,755
    Thanks!

    but i've done some changes :P

    its now possible to save as project file and image file
    and loading is also possible, importing BG pics, deleting lines
    coloring lines etc...

    check this out
    Attached Files Attached Files
    Check out the FAQ and do a search before you post.
    My tutorials: Anti-Alias Pixels, Accurate Game Loop, Resource File

  20. #20

    Thread Starter
    Frenzied Member cyborg's Avatar
    Join Date
    May 2000
    Location
    Sweden
    Posts
    1,755
    it would be really cool to AA the lines!
    does anyone here know how to do that?

    i can attach the source if you want...
    Check out the FAQ and do a search before you post.
    My tutorials: Anti-Alias Pixels, Accurate Game Loop, Resource File

  21. #21

    Thread Starter
    Frenzied Member cyborg's Avatar
    Join Date
    May 2000
    Location
    Sweden
    Posts
    1,755
    i'll post this as a new thread!
    Check out the FAQ and do a search before you post.
    My tutorials: Anti-Alias Pixels, Accurate Game Loop, Resource File

  22. #22
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573
    I see you are really speeding your way along the Bezier road!

    In your original post below, I assume you referred to the behaviour of the points when you use the 'smooth' tool. I may take a deeper look at it as soon as I have some time... in case you're still interested.

    Originally posted by cyborg
    hey!
    i found out a way to do it the first way i suggested, but somethings a bit wrong...

    ill attach the source for you to see whats wrong...
    pretty bad writted code, but you'll probably understand it.

  23. #23

    Thread Starter
    Frenzied Member cyborg's Avatar
    Join Date
    May 2000
    Location
    Sweden
    Posts
    1,755
    yes im still interested!
    Check out the FAQ and do a search before you post.
    My tutorials: Anti-Alias Pixels, Accurate Game Loop, Resource File

  24. #24
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573
    Hmmm... I think I'll take a look at it over the week end.

  25. #25

    Thread Starter
    Frenzied Member cyborg's Avatar
    Join Date
    May 2000
    Location
    Sweden
    Posts
    1,755
    ok, thanks alot!
    Check out the FAQ and do a search before you post.
    My tutorials: Anti-Alias Pixels, Accurate Game Loop, Resource File

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width