Author Topic: Simple coordinates equation or impossible coordinates equation?  (Read 7620 times)

0 Members and 1 Guest are viewing this topic.

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Ever since the 90's I've tried various ways to make this equation which probably seems laughable to you all because the question is so simple.
How can I plot points automatically from any coordinate to any other coordinate in a straight line, without using the LINE command?
I am trying to make a cone making program using the CIRCLE command and I not only need that answer, but also how to make a smooth rendition in radius from one to the other.
Does anyone know or can point me to somewhere that knows? Thanks. All of my games that involve a bad guy chasing you is just the simple (for example) if x < x2 then x=x+1. But it's not that easy when making a straight line when y is different of course. It's just been so many decades and I've looked on Google and I can't really find a way. There must be some kind of equation?

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Simple coordinates equation or impossible coordinates equation?
« Reply #1 on: December 14, 2019, 07:51:39 pm »
Wouldn’t you just use the slope?

Rise = Y1 - Y2
Run = X1 - X2
Slope = Rise / Run

Then each time you move X steps, you increment Y by the slope * X.

Or move Y steps, you increment X by 1 / slope * Y.

(Or maybe vice versa.  It’s been a while, so I may have X/Y reversed.)
« Last Edit: December 14, 2019, 08:06:28 pm by SMcNeill »
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline OldMoses

  • Seasoned Forum Regular
  • Posts: 469
    • View Profile
Re: Simple coordinates equation or impossible coordinates equation?
« Reply #2 on: December 14, 2019, 08:05:31 pm »
I concur with Steve on that one. Even dealing with a three dimensional environment, the principle of the thing should hold true. It only gets a little more difficult if you start converting between cartesian coordinates and polar ones. Then the trig rears its head.

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Simple coordinates equation or impossible coordinates equation?
« Reply #3 on: December 14, 2019, 08:16:07 pm »
LOL! I knew it had to be easy. Thanks guys! I will work on that probably tonight or tomorrow. I just been out of school for a very long time. lol

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Simple coordinates equation or impossible coordinates equation?
« Reply #4 on: December 14, 2019, 09:15:07 pm »
For some reason I can't get it to work. I've tried different variations and still nothing. I don't know if it's a negative and positive thing or what it could be. Here is the code I have so far. The first half is just asking people for the 2 sets of coordinates. I might be missing something huge or small.

Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(800, 600, 32)
  2. start:
  3. _TITLE "Cone Maker - by Ken G."
  4. _LIMIT 200
  5. x1:
  6. INPUT "X1 (0-800) = ", x1
  7. IF x1 < 0 OR x1 > 800 THEN PRINT "Out of range, try again.": GOTO x1:
  8. y1:
  9. INPUT "Y1 (0-600) = ", y1
  10. IF y1 < 0 OR y1 > 600 THEN PRINT "Out of range, try again.": GOTO y1:
  11. r1:
  12. INPUT "Radius (1-400) = ", r1
  13. IF r1 < 1 OR r1 > 400 THEN PRINT "Out of range, try again.": GOTO r1:
  14. x2:
  15. INPUT "X2 (0-800) = ", x2
  16. IF x2 < 0 OR x2 > 800 THEN PRINT "Out of range, try again.": GOTO x2:
  17. y2:
  18. INPUT "Y2 (0-600) = ", y2
  19. IF y2 < 0 OR y2 > 600 THEN PRINT "Out of range, try again.": GOTO y2:
  20. r2:
  21. INPUT "Radius (1-400) = ", r2
  22. IF r2 < 1 OR r2 > 400 THEN PRINT "Out of range, try again.": GOTO r2:
  23. '-----------------------------------------------------------------------------------------------
  24. rn = x1 - x2
  25. rs = y1 - y2
  26. slope = rs / rn
  27. c = 50
  28.     x1 = slope * y1
  29.     y1 = 1 / slope * x1
  30.     c = c + 1
  31.     CIRCLE (x1, y1), 10, _RGB32(0, c, 0)
  32.     IF x1 > x2 - 2 AND x1 < x2 + 2 AND y1 > y2 - 2 AND y1 < y2 + 2 THEN END
  33.  
  34.  

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Simple coordinates equation or impossible coordinates equation?
« Reply #5 on: December 14, 2019, 09:31:48 pm »
Let’s say we want to draw a line from 100,100 to 200,200.

Your slope is going to end up being 1, so all you need is:

FOR I = 0 TO 100
    PSET (X1 + I,  Y1 + I * Slope)
NEXT

^ Each time your X-coordinate increases a step, you increase the Y-coordinate by the slope.

You’d plot at:
100,100
101,101
102,102
...
198,198
199,199
200,200

*Im not at a PC tonight, but I’ll write you an actual demo tomorrow, if no one else gets around to it before then.  ;)
« Last Edit: December 14, 2019, 09:33:20 pm by SMcNeill »
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline OldMoses

  • Seasoned Forum Regular
  • Posts: 469
    • View Profile
Re: Simple coordinates equation or impossible coordinates equation?
« Reply #6 on: December 14, 2019, 09:47:24 pm »
If I'm understanding this project correctly, getting slope is only the beginning of the problem. Creating a cone from a circle brings in issues of perspective. What direction is the cone viewed from? Above, sidelong or obliquely? It's hard to represent a solid well without going into 3D (adding a Z axis to the equation), then the shading and/or ray tracing algorithm would have something to be based upon.

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Simple coordinates equation or impossible coordinates equation?
« Reply #7 on: December 14, 2019, 10:02:44 pm »
I think I might have got it! Or it's as close as I can get it so far. It's probably not perfect, might not even be perfect coordinates. And I know it's not perfect radius, might work with that later tomorrow.

Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(800, 600, 32)
  2. start:
  3. _TITLE "Cone Maker - by Ken G."
  4. _LIMIT 200
  5. x1:
  6. INPUT "X1 (0-800) = ", x1
  7. IF x1 < 0 OR x1 > 800 THEN PRINT "Out of range, try again.": GOTO x1:
  8. y1:
  9. INPUT "Y1 (0-600) = ", y1
  10. IF y1 < 0 OR y1 > 600 THEN PRINT "Out of range, try again.": GOTO y1:
  11. r1:
  12. INPUT "Radius (1-400) = ", r1
  13. IF r1 < 1 OR r1 > 400 THEN PRINT "Out of range, try again.": GOTO r1:
  14. x2:
  15. INPUT "X2 (0-800) = ", x2
  16. IF x2 < 0 OR x2 > 800 THEN PRINT "Out of range, try again.": GOTO x2:
  17. y2:
  18. INPUT "Y2 (0-600) = ", y2
  19. IF y2 < 0 OR y2 > 600 THEN PRINT "Out of range, try again.": GOTO y2:
  20. r2:
  21. INPUT "Radius (1-400) = ", r2
  22. IF r2 < 1 OR r2 > 400 THEN PRINT "Out of range, try again.": GOTO r2:
  23. gap:
  24. INPUT "Gap (.01-1) = ", gap
  25. IF gap < .01 OR gap > 40 THEN PRINT "Out of range, try again.": GOTO r2:
  26.  
  27.  
  28. rn = x1 - x2
  29. rs = y1 - y2
  30. slope = rs / rn
  31. c = 50
  32.     x1 = slope + x1
  33.     y1 = slope + y1
  34.     r1 = slope + r1
  35.     c = c + 1
  36.     CIRCLE (x1, y1), r1, _RGB32(0, c, 0)
  37.     IF x1 > x2 - 5 AND x1 < x2 + 5 AND y1 > y2 - 5 AND y1 < y2 + 5 THEN END
  38.  
  39.  
  40.  





Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Simple coordinates equation or impossible coordinates equation?
« Reply #8 on: December 14, 2019, 11:05:01 pm »
This?
Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(800, 600, 32)
  2. x1 = 200
  3. y1 = 50
  4. x2 = 300
  5. y2 = 300
  6. CIRCLE (x1, y1), 1
  7. CIRCLE (x2, y2), 1
  8. dx = x2 - x1
  9. dy = y2 - y1
  10.  
  11. 'y1 = x1 * dy/dx - yIntersect
  12. yIntersect = x1 * dy / dx - y1
  13.  
  14. FOR x = x1 TO x2
  15.     PSET (x, x * dy / dx - yIntersect)
  16.  
  17. 'cone  axis ?
  18. 'from y = 50 to y = 300 x = 200
  19. LINE (200, 50)-(200, 300)
  20.  
  21. 'circles  of cone will have radius x - 200
  22. 'circles of cone will have centers x = 200 y=some where between 50 and 300
  23. FOR x = x1 TO x2 STEP 10
  24.     CIRCLE (200, x * dy / dx - yIntersect), x - 200, &HFF008800, , , .1
  25.  
  26.  

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Simple coordinates equation or impossible coordinates equation?
« Reply #9 on: December 14, 2019, 11:35:45 pm »
LOL bplus, you just got the Christmas Tree Award! Pretty awesome there.
I was more actually looking for 2 circles making a cone with 2 open ends. But I can try to work with yours sometime, thanks!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Simple coordinates equation or impossible coordinates equation?
« Reply #10 on: December 15, 2019, 12:45:01 am »
Pretty interesting Interference patterns:
Code: QB64: [Select]
  1. _TITLE "Cone Trees,   press any for new scene..."
  2. CONST xmax = 1280, ymax = 720, ymaxd2 = ymax / 2
  3. SCREEN _NEWIMAGE(xmax, ymax, 32)
  4.     FOR y = 0 TO ymax / 2
  5.         LINE (0, y)-(xmax, y), _RGB32(200, 100, 128 + 127 * y / ymaxd2), BF
  6.     NEXT
  7.     FOR y = ymax / 2 TO ymax
  8.         yy = y - ymaxd2
  9.  
  10.         LINE (0, y)-(xmax, y), _RGB32(255 - 128 * yy / ymaxd2, 255 - 128 * yy / ymaxd2, 255 - 128 * yy / ymaxd2), BF
  11.     NEXT
  12.     FOR i = 1 TO 100
  13.         x = RND * xmax:
  14.         h = randWeight(10, ymax - 20, 8)
  15.         y = (ymax - h) / 2: s = 2 * RND + 2
  16.         drawCone x, y, h, s
  17.     NEXT
  18.     SLEEP
  19.  
  20. SUB drawCone (topX, topY, height, slope) 'slope 2 to 4 probably best
  21.     x1 = topX
  22.     y1 = topY
  23.     y2 = y1 + height
  24.     yIntersect = x1 * slope - y1
  25.     LINE (x1, y1)-(x1, y2), &HFF884422
  26.     FOR y = y1 TO y2 STEP 2
  27.         x = (y + yIntersect) / slope
  28.         CIRCLE (x1, y), x - x1, &H77008800, _PI, 2 * _PI, .1
  29.     NEXT
  30.  
  31. FUNCTION randWeight (manyValue, fewValue, power)
  32.     randWeight = manyValue + RND ^ power * (fewValue - manyValue)
  33.  
  34.  


Quote
I was more actually looking for 2 circles making a cone with 2 open ends.
Ken can you find a picture of what you are talking about?
A mega horn shape?
« Last Edit: December 15, 2019, 12:55:46 am by bplus »

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: Simple coordinates equation or impossible coordinates equation?
« Reply #11 on: December 15, 2019, 04:02:55 am »
Beautiful as usual bplus. I like when you take someone's code and run with it.

So ken, does the picture below kindof sketch what you want?

What you see are two ellipses (drawn by hand in paint, but we have the real thing in qb64), and then two lines. The lines don't necessarily touch the top of the ellipses, or any particular axis. Rather, they are a pair of tangent lines that just brush the two ellipses. The second screenshot shows a second case to help paint the picture.

To calculate those tangent lines, there are two methods... I bet I can predict the type of answers you'll receive - provided these pictures get close to your question... but think that Lagrange multipliers are the way to go. I'll cook up the latter solution for an exercise perhaps.
kencone.png
* kencone.png (Filesize: 4.49 KB, Dimensions: 589x349, Views: 158)
kencone2.png
* kencone2.png (Filesize: 4.6 KB, Dimensions: 589x349, Views: 147)
You're not done when it works, you're done when it's right.

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: Simple coordinates equation or impossible coordinates equation?
« Reply #12 on: December 15, 2019, 08:10:59 am »
Hi SierraKen,

Try GETPOINTS  SUB, which i wrote for you now. Sub return array, which contains all points between two points (so as LINE it use)

Code: QB64: [Select]
  1. 'LINE points to array
  2.  
  3. TYPE LinePoints
  4.     X AS INTEGER
  5.     Y AS INTEGER
  6. REDIM SHARED LP(0) AS LinePoints
  7.  
  8. SCREEN _NEWIMAGE(800, 600, 32)
  9.  
  10. LINE (540, 99)-(100, 199)
  11. GETPOINTS 540, 99, 100, 199, LP()
  12. 'if all points in array are calctulated correctly, all points in LINE are draw yellow
  13.  
  14.  
  15. FOR D = 0 TO UBOUND(lp)
  16.     PSET (LP(D).X, LP(D).Y), &HFFFFFF00
  17.  
  18.  
  19. SUB GETPOINTS (x1, y1, x2, y2, A() AS LinePoints)
  20.     DIM lenght AS INTEGER
  21.     lenght = _HYPOT(x1 - x2, y1 - y2) 'Fellippe Heitor show me using this great function.
  22.     REDIM A(lenght) AS LinePoints
  23.     FOR fill = 0 TO lenght
  24.         IF x1 > x2 THEN A(fill).X = x1 - fill * ((x1 - x2) / lenght)
  25.         IF x1 < x2 THEN A(fill).X = x1 + fill * ((x2 - x1) / lenght)
  26.         IF x1 = x2 THEN A(fill).X = x1
  27.         IF y1 > y2 THEN A(fill).Y = y1 - fill * ((y1 - y2) / lenght)
  28.         IF y1 < y2 THEN A(fill).Y = y1 + fill * ((y2 - y1) / lenght)
  29.         IF y1 = y2 THEN A(fill).Y = y1
  30.     NEXT
  31.  
´

And for drawing circles and elipses...  here is one small part from my one program for QB64 draw... i am too lazy to degrade it for easiest use... For CIRCLE use, set CIRCLES size to 1.

Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(800, 600, 32)
  2.  
  3.     Circles &HFFFF0000, 10
  4.  
  5. SUB Circles (FColor~&, size)
  6.     InitMouse mx, my, mb1, mb2
  7.     omx = mx: omy = my
  8.     PCOPY _DISPLAY, 1
  9.  
  10.     DO UNTIL mb1 = 0
  11.         InitMouse mx, my, mb1, mb2
  12.         PCOPY 1, _DISPLAY
  13.         k& = _KEYHIT
  14.  
  15.         CX = (omx + mx) / 2
  16.         CY = (omy + my) / 2
  17.  
  18.         r1 = ABS((omx - mx) / 2)
  19.         r2 = ABS((omy - my) / 2)
  20.  
  21.         ratio = ABS((omy - my) / (omx - mx))
  22.         IF k& = 32 THEN
  23.             ratio = 1
  24.             IF r1 >= r2 THEN r = r1 ELSE r = r2
  25.             IF omx < mx THEN CX = omx + r ELSE CX = omx - r
  26.             IF omy < my THEN CY = omy + r ELSE CY = omy - r
  27.  
  28.             IF mx <= omx THEN omx = CX - r: mx = CX + r
  29.             IF mx > omx THEN omx = CX + r: mx = CX - r
  30.  
  31.             IF my <= omy THEN omy = CY - r: my = CY + r
  32.             IF my > omy THEN omy = CY + r: my = CY - r
  33.         END IF
  34.         IF r1 >= r2 THEN r = r1 ELSE r = r2
  35.         IF omx < mx THEN LineX1 = omx - size / 2: LineX2 = mx + size / 2 ELSE LineX1 = omx + size / 2: LineX2 = mx - size / 2
  36.         IF omy < my THEN LineY1 = omy - size / 2: LineY2 = my + size / 2 ELSE LineY1 = omy + size / 2: LineY2 = my - size / 2
  37.         LINE (LineX1, LineY1)-(LineX2, LineY2), &HFF888888, B , 127
  38.  
  39.  
  40.  
  41.  
  42.         IF size = 1 THEN
  43.             CIRCLE (CX, CY), r, FColor~&, , , ratio
  44.         ELSE
  45.             FOR k = 0 TO _PI(2) STEP .01
  46.                 x1 = CX + COS(k) * r1
  47.                 y1 = CY + SIN(k) * r2
  48.                 IF k& = 32 THEN
  49.                     rr1 = r
  50.                     rr2 = r
  51.                 ELSE
  52.                     rr1 = r1
  53.                     rr2 = r2
  54.                 END IF
  55.  
  56.                 x2 = CX + COS(k + .035) * rr1
  57.                 y2 = CY + SIN(k + .035) * rr2
  58.  
  59.                 TLine x1, y1, x2, y2, FColor~&, size
  60.  
  61.             NEXT k
  62.         END IF
  63.  
  64.         _DISPLAY
  65.         _LIMIT 20
  66.     LOOP
  67.  
  68. SUB TLine (x1, y1, x2, y2, kolor~&, size)
  69.     lenght = SQR((x2 - x1) ^ 2 + (y2 - y1) ^ 2)
  70.     '    angle = JK!(x1, y1, x2, y2, lenght)
  71.     angle = JK2!(x1, y1, x2, y2)
  72.  
  73.     x5 = x1 + COS(angle - _PI(.5)) * size / 2
  74.     y5 = y1 + SIN(angle - _PI(.5)) * size / 2
  75.     x6 = x2 + COS(angle - _PI(.5)) * size / 2
  76.     y6 = y2 + SIN(angle - _PI(.5)) * size / 2
  77.  
  78.     x3 = x1 + COS(angle + _PI(.5)) * size / 2 'Calculation of perpendicular vector. The default vector is X1, Y1 to X2, Y2. The JK function returns the angle of rotation of the vector, the perpendicular vector is shift by 90 degrees, (or PI /2) in radians.
  79.     y3 = y1 + SIN(angle + _PI(.5)) * size / 2
  80.     x4 = x2 + COS(angle + _PI(.5)) * size / 2
  81.     y4 = y2 + SIN(angle + _PI(.5)) * size / 2
  82.  
  83.     v = _NEWIMAGE(100, 100, 32)
  84.     d = _DEST
  85.     _DEST v
  86.     CLS , kolor~&
  87.     _DEST d
  88.  
  89.     IF lenght THEN
  90.         _MAPTRIANGLE (1, 1)-(99, 1)-(1, 99), v TO(x5, y5)-(x6, y6)-(x3, y3), _DEST, _SMOOTH
  91.         _MAPTRIANGLE (99, 1)-(1, 99)-(99, 99), v TO(x6, y6)-(x3, y3)-(x4, y4), _DEST, _SMOOTH
  92.     END IF
  93.     _FREEIMAGE v
  94.  
  95.  
  96. SUB InitMouse (mx, my, mb1, mb2)
  97.     mx = _MOUSEX
  98.     my = _MOUSEY
  99.     mb1 = _MOUSEBUTTON(1)
  100.     mb2 = _MOUSEBUTTON(2)
  101.  
  102. FUNCTION JK2! (x1, y1, x2, y2) 'X = cos, Y = sin
  103.     JK2! = _ATAN2(y1 - y2, x1 - x2)
  104.     IF JK2! < 0 THEN JK2! = _PI(2) + JK2!
  105.  


I add here simple example how use CIRCLE collision detection and GETPOINTS.  The output is not 100% accurate.

Code: QB64: [Select]
  1. TYPE LinePoints
  2.     X AS INTEGER
  3.     Y AS INTEGER
  4. REDIM SHARED a(0) AS LinePoints 'contains X, Y for 1 LINE (40 points)
  5. REDIM SHARED B(0) AS LinePoints 'contains X, Y for bottom and ceilings lines (40 points * 20 * 2)
  6. REDIM SHARED c(0) AS LinePoints 'It contains points X, Y in a square cutout around the balloon
  7.  
  8. SCREEN _NEWIMAGE(800, 600, 32)
  9.  
  10.  
  11. oldy = 40 + RND * 80
  12. FOR u = 0 TO 760 STEP 40
  13.     oldy = y
  14.     y = 40 + RND * 150
  15.     LINE (u, oldy)-(u + 40, y)
  16.     GETPOINTS u, oldy, u + 40, y, a() 'save all LINE points to array A (ceiling)
  17.     ADDPOINTS a(), B() '                         add this points to array B
  18.  
  19. y = 560 - RND * 80
  20. FOR u = 0 TO 760 STEP 40
  21.     oldy = y
  22.     y = 560 - RND * 130
  23.     LINE (u, oldy)-(u + 40, y)
  24.     GETPOINTS u, oldy, u + 40, y, a() 'save all LINE points to array A (bottom)
  25.     ADDPOINTS a(), B() '                         add this points to array B
  26.  
  27.  
  28. TYPE ball
  29.     X AS INTEGER
  30.     Y AS INTEGER
  31.     Xs AS INTEGER
  32.     Ys AS INTEGER
  33. DIM Ball AS ball
  34.  
  35. Ball.X = 400
  36. Ball.Y = 300
  37. Ball.Xs = 1
  38. Ball.Ys = 1
  39.  
  40.     PCOPY 1, _DISPLAY
  41.     SORT Ball.X, Ball.Y, c() '           write VALIDS points for compars from array B to array C
  42.     FOR test = 0 TO UBOUND(c)
  43.         x = c(test).X
  44.         y = c(test).Y
  45.         xy& = _HYPOT(x - Ball.X, y - Ball.Y)
  46.         IF 50 ^ 2 >= xy& THEN
  47.             IF Ball.Y < 300 THEN Ball.Ys = 1 + ABS(_ATAN2(Ball.Ys - y, Ball.Xs - x)) ELSE Ball.Ys = -1 - ABS(_ATAN2(Ball.Ys - y, Ball.Xs - x))
  48.         END IF
  49.     NEXT
  50.  
  51.     IF Ball.X - 50 < 1 THEN Ball.Xs = 1
  52.     IF Ball.X + 50 > 800 THEN Ball.Xs = -1
  53.     IF Ball.Y - 50 < 1 THEN Ball.Ys = 1
  54.     IF Ball.Y + 50 > 600 THEN Ball.Ys = -1
  55.  
  56.  
  57.  
  58.     CIRCLE (Ball.X, Ball.Y), 50
  59.     Ball.X = Ball.X + Ball.Xs
  60.     Ball.Y = Ball.Y + Ball.Ys
  61.  
  62.     _DISPLAY
  63.     _LIMIT 30
  64.  
  65. SUB SORT (x, y, c() AS LinePoints)
  66.     REDIM c(0) AS LinePoints
  67.     FOR t = 0 TO UBOUND(b)
  68.         IF B(t).X > x - 50 AND B(t).X < x + 50 THEN
  69.             IF B(t).Y > y - 50 AND B(t).Y < y + 50 THEN
  70.                 c(i).X = B(t).X
  71.                 c(i).Y = B(t).Y
  72.                 i = i + 1
  73.                 REDIM _PRESERVE c(i) AS LinePoints
  74.             END IF
  75.         END IF
  76.     NEXT
  77.     REDIM _PRESERVE c(i - 1) AS LinePoints
  78.  
  79. SUB GETPOINTS (x1, y1, x2, y2, A() AS LinePoints)
  80.     DIM lenght AS INTEGER
  81.     lenght = _HYPOT(x1 - x2, y1 - y2) 'Fellippe Heitor show me using this great function.
  82.     REDIM A(lenght) AS LinePoints
  83.     FOR fill = 0 TO lenght
  84.         IF x1 > x2 THEN A(fill).X = x1 - fill * ((x1 - x2) / lenght)
  85.         IF x1 < x2 THEN A(fill).X = x1 + fill * ((x2 - x1) / lenght)
  86.         IF x1 = x2 THEN A(fill).X = x1
  87.         IF y1 > y2 THEN A(fill).Y = y1 - fill * ((y1 - y2) / lenght)
  88.         IF y1 < y2 THEN A(fill).Y = y1 + fill * ((y2 - y1) / lenght)
  89.         IF y1 = y2 THEN A(fill).Y = y1
  90.     NEXT
  91.  
  92. SUB ADDPOINTS (a() AS LinePoints, B() AS LinePoints)
  93.     u = UBOUND(b)
  94.     u2 = UBOUND(a)
  95.     REDIM _PRESERVE B(u + u2) AS LinePoints
  96.     FOR add = u TO u + u2
  97.         B(add) = a(i)
  98.         i = i + 1
  99.     NEXT
  100.  

 
collision.JPG


« Last Edit: December 15, 2019, 11:19:25 am by Petr »

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Simple coordinates equation or impossible coordinates equation?
« Reply #13 on: December 15, 2019, 12:31:48 pm »
LOL Bplus, really cool forest!

Peter, those are some really great examples of finding coordinates, thanks! I might use your line and circle detection on some games if I get around to it. Thanks again. :)

Static, yes that's what I'm making. A person types in 2 X and Y coordinates and it makes 2 circles which are both ends of the cone. Then it draws more circles to go from one end to the other to form the cone. I tried also doing it with my circle code and using the LINE command at each point, going from 1 circle to the other. And it SORTA works. Except that most cones don't come out very good when the LINES go all the way around. So I changed it to the CIRCLE command, and that is how I needed the SLOPE equation and am still working with that. I am almost there though, will have to work with it later this afternoon. This is what I have so far..

By the way, just so you all know, I'm not in the process of making any huge program or game with this, am just making a Cone making program is all. And I thought that this would be good learning experience for myself and maybe others.

Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(800, 600, 32)
  2. start:
  3. _TITLE "Cone Maker - by Ken G."
  4. _LIMIT 200
  5. x1:
  6. INPUT "X1 (0-800) = ", x1
  7. IF x1 < 0 OR x1 > 800 THEN PRINT "Out of range, try again.": GOTO x1:
  8. y1:
  9. INPUT "Y1 (0-600) = ", y1
  10. IF y1 < 0 OR y1 > 600 THEN PRINT "Out of range, try again.": GOTO y1:
  11. r1:
  12. INPUT "Radius (1-400) = ", r1
  13. IF r1 < 1 OR r1 > 400 THEN PRINT "Out of range, try again.": GOTO r1:
  14. x2:
  15. INPUT "X2 (0-800) = ", x2
  16. IF x2 < 0 OR x2 > 800 THEN PRINT "Out of range, try again.": GOTO x2:
  17. y2:
  18. INPUT "Y2 (0-600) = ", y2
  19. IF y2 < 0 OR y2 > 600 THEN PRINT "Out of range, try again.": GOTO y2:
  20. r2:
  21. INPUT "Radius (1-400) = ", r2
  22. IF r2 < 1 OR r2 > 400 THEN PRINT "Out of range, try again.": GOTO r2:
  23. gap:
  24. INPUT "Gap (.01-1) = ", gap
  25. IF gap < .01 OR gap > 40 THEN PRINT "Out of range, try again.": GOTO r2:
  26.  
  27.  
  28. rn = x1 - x2
  29. rs = y1 - y2
  30. slope = rs / rn
  31. c = 50
  32.     x1 = slope + x1
  33.     y1 = slope + y1
  34.     r1 = slope + r1
  35.     c = c + 1
  36.     CIRCLE (x1, y1), r1, _RGB32(0, c, 0)
  37.     IF x1 > x2 - 5 AND x1 < x2 + 5 AND y1 > y2 - 5 AND y1 < y2 + 5 THEN END
  38.  
  39.  

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Simple coordinates equation or impossible coordinates equation?
« Reply #14 on: December 15, 2019, 12:46:54 pm »
Hi STxAxTIC,

I discovered that this works only part of the time, from ToolBox:
Code: QB64: [Select]
  1. 'STxAxTIC this is not filling correctly all the time, only sometimes works
  2. SUB EllipseTiltFill (destHandle&, CX, CY, a, b, ang, C AS _UNSIGNED LONG)
  3.     '  destHandle& = destination handle
  4.     '  CX = center x coordinate
  5.     '  CY = center y coordinate
  6.     '   a = semimajor axis
  7.     '   b = semiminor axis
  8.     ' ang = clockwise orientation of semimajor axis in radians (0 default)
  9.     '   C = fill color
  10.     DIM max AS INTEGER, mx2 AS INTEGER, i AS INTEGER, j AS INTEGER
  11.     DIM prc AS _UNSIGNED LONG
  12.     DIM D AS INTEGER, S AS INTEGER
  13.     D = _DEST: S = _SOURCE
  14.     prc = _RGB32(255, 255, 255, 255)
  15.     IF a > b THEN max = a + 1 ELSE max = b + 1
  16.     mx2 = max + max
  17.     tef& = _NEWIMAGE(mx2, mx2)
  18.     _DEST tef&
  19.     _SOURCE tef&
  20.     FOR k = 0 TO 6.283185307179586 + .025 STEP .025
  21.         i = max + a * COS(k) * COS(ang) + b * SIN(k) * SIN(ang)
  22.         j = max + a * COS(k) * SIN(ang) - b * SIN(k) * COS(ang)
  23.         IF k <> 0 THEN
  24.             LINE (lasti, lastj)-(i, j), prc
  25.         ELSE
  26.             PSET (i, j), prc
  27.         END IF
  28.         lasti = i: lastj = j
  29.     NEXT
  30.     DIM xleft(mx2) AS INTEGER, xright(mx2) AS INTEGER, x AS INTEGER, y AS INTEGER
  31.     FOR y = 0 TO mx2
  32.         x = 0
  33.         WHILE POINT(x, y) <> prc AND x < mx2
  34.             x = x + 1
  35.         WEND
  36.         xleft(y) = x
  37.         WHILE POINT(x, y) = prc AND x < mx2
  38.             x = x + 1
  39.         WEND
  40.         WHILE POINT(x, y) <> prc AND x < mx2
  41.             x = x + 1
  42.         WEND
  43.         IF x = mx2 THEN xright(y) = xleft(y) ELSE xright(y) = x
  44.     NEXT
  45.     _DEST destHandle&
  46.     FOR y = 0 TO mx2
  47.         IF xleft(y) <> mx2 THEN LINE (xleft(y) + CX - max, y + CY - max)-(xright(y) + CX - max, y + CY - max), C, BF
  48.     NEXT
  49.     _DEST D: _DEST S
  50.     _FREEIMAGE tef&


Here is my test code for drawing Megaphones:
Code: QB64: [Select]
  1. _TITLE "Draw megaphone Test, click 4 points in clockwise direction first pair is one opening and 2nd is 2nd." 'B+ 2019-12-15
  2. CONST xmax = 800, ymax = 600
  3. SCREEN _NEWIMAGE(xmax, ymax, 32)
  4. _SCREENMOVE 300, 40
  5.  
  6.     CLS
  7.     WHILE pi < 4 'get 4 mouse clicks >>>>>>>>>>>>>>>>>>>> HERE pi means Point Index not _PI
  8.         _PRINTSTRING (5, 5), SPACE$(20)
  9.         _PRINTSTRING (5, 5), "Need 4 clicks, have" + STR$(pi)
  10.         WHILE _MOUSEINPUT: WEND
  11.         IF _MOUSEBUTTON(1) AND oldMouse = 0 THEN 'new mouse down
  12.             pi = pi + 1
  13.             mx(pi) = _MOUSEX: my(pi) = _MOUSEY
  14.             CIRCLE (mx(pi), my(pi)), 2
  15.         END IF
  16.         oldMouse = _MOUSEBUTTON(1)
  17.         _DISPLAY
  18.         _LIMIT 60
  19.     WEND
  20.     LINE (mx(2), my(2))-(mx(3), my(3))
  21.     LINE (mx(4), my(4))-(mx(1), my(1))
  22.     ang1 = _ATAN2(my(2) - my(1), mx(2) - mx(1))
  23.     maj1 = (_HYPOT(mx(1) - mx(2), my(1) - my(2))) / 2
  24.     min1 = .1 * maj1
  25.     cx1 = (mx(1) + mx(2)) / 2
  26.     cy1 = (my(1) + my(2)) / 2
  27.     EllipseTilt cx1, cy1, maj1, min1, ang1, &HFF0000FF
  28.     ang2 = _ATAN2(my(3) - my(4), mx(3) - mx(4))
  29.     maj2 = (_HYPOT(mx(3) - mx(4), my(3) - my(4))) / 2
  30.     min2 = .1 * maj2
  31.     cx2 = (mx(3) + mx(4)) / 2
  32.     cy2 = (my(3) + my(4)) / 2
  33.     EllipseTilt cx2, cy2, maj2, min2, ang2, &HFF0000FF
  34.     _DISPLAY
  35.     _DELAY 3
  36.     CLS
  37.     EllipseTiltFill 0, cx2, cy2, maj2, min2, ang2, &HFF0000FF
  38.     fquad mx(1), my(1), mx(2), my(2), mx(3), my(3), mx(4), my(4), &HFF0000FF
  39.     EllipseTiltFill 0, cx1, cy1, maj1, min1, ang1, &HFFFF0000
  40.     _DISPLAY
  41.     _DELAY 3
  42.  
  43.     pi = 0 'point index
  44.  
  45.  
  46.  
  47. 'thanks STxAxTIC from Toolbox
  48. SUB EllipseTilt (CX, CY, a, b, ang, C AS _UNSIGNED LONG)
  49.     DIM k, i, j
  50.     '  CX = center x coordinate
  51.     '  CY = center y coordinate
  52.     '   a = semimajor axis  major radius
  53.     '   b = semiminor axis  minor radius
  54.     ' ang = clockwise orientation of semimajor axis in radians (0 default)
  55.     '   C = fill color
  56.     FOR k = 0 TO 6.283185307179586 + .025 STEP .025
  57.         i = a * COS(k) * COS(ang) + b * SIN(k) * SIN(ang)
  58.         j = -a * COS(k) * SIN(ang) + b * SIN(k) * COS(ang)
  59.         i = i + CX
  60.         j = -j + CY
  61.         IF k <> 0 THEN
  62.             LINE -(i, j), C, BF
  63.         ELSE
  64.             PSET (i, j), C
  65.         END IF
  66.     NEXT
  67.  
  68. 'STxAxTIC this is not filling correctly all the time, only sometimes works
  69. SUB EllipseTiltFill (destHandle&, CX, CY, a, b, ang, C AS _UNSIGNED LONG)
  70.     '  destHandle& = destination handle
  71.     '  CX = center x coordinate
  72.     '  CY = center y coordinate
  73.     '   a = semimajor axis
  74.     '   b = semiminor axis
  75.     ' ang = clockwise orientation of semimajor axis in radians (0 default)
  76.     '   C = fill color
  77.     DIM max AS INTEGER, mx2 AS INTEGER, i AS INTEGER, j AS INTEGER
  78.     DIM prc AS _UNSIGNED LONG
  79.     DIM D AS INTEGER, S AS INTEGER
  80.     D = _DEST: S = _SOURCE
  81.     prc = _RGB32(255, 255, 255, 255)
  82.     IF a > b THEN max = a + 1 ELSE max = b + 1
  83.     mx2 = max + max
  84.     tef& = _NEWIMAGE(mx2, mx2)
  85.     _DEST tef&
  86.     _SOURCE tef&
  87.     FOR k = 0 TO 6.283185307179586 + .025 STEP .025
  88.         i = max + a * COS(k) * COS(ang) + b * SIN(k) * SIN(ang)
  89.         j = max + a * COS(k) * SIN(ang) - b * SIN(k) * COS(ang)
  90.         IF k <> 0 THEN
  91.             LINE (lasti, lastj)-(i, j), prc
  92.         ELSE
  93.             PSET (i, j), prc
  94.         END IF
  95.         lasti = i: lastj = j
  96.     NEXT
  97.     DIM xleft(mx2) AS INTEGER, xright(mx2) AS INTEGER, x AS INTEGER, y AS INTEGER
  98.     FOR y = 0 TO mx2
  99.         x = 0
  100.         WHILE POINT(x, y) <> prc AND x < mx2
  101.             x = x + 1
  102.         WEND
  103.         xleft(y) = x
  104.         WHILE POINT(x, y) = prc AND x < mx2
  105.             x = x + 1
  106.         WEND
  107.         WHILE POINT(x, y) <> prc AND x < mx2
  108.             x = x + 1
  109.         WEND
  110.         IF x = mx2 THEN xright(y) = xleft(y) ELSE xright(y) = x
  111.     NEXT
  112.     _DEST destHandle&
  113.     FOR y = 0 TO mx2
  114.         IF xleft(y) <> mx2 THEN LINE (xleft(y) + CX - max, y + CY - max)-(xright(y) + CX - max, y + CY - max), C, BF
  115.     NEXT
  116.     _DEST D: _DEST S
  117.     _FREEIMAGE tef&
  118.  
  119. '2019-11-20 Steve saves some time with STATIC and saves and restores last dest
  120. SUB ftri (x1, y1, x2, y2, x3, y3, K AS _UNSIGNED LONG)
  121.     DIM D AS LONG
  122.     STATIC a&
  123.     D = _DEST
  124.     IF a& = 0 THEN a& = _NEWIMAGE(1, 1, 32)
  125.     _DEST a&
  126.     PSET (0, 0), K
  127.     _DEST D
  128.     _MAPTRIANGLE _SEAMLESS(0, 0)-(0, 0)-(0, 0), a& TO(x1, y1)-(x2, y2)-(x3, y3)
  129.  
  130. 'need 4 non linear points (not all on 1 line) list them clockwise so x2, y2 is opposite of x4, y4
  131. SUB fquad (x1, y1, x2, y2, x3, y3, x4, y4, K AS _UNSIGNED LONG)
  132.     ftri x1, y1, x2, y2, x4, y4, K
  133.     ftri x3, y3, x2, y2, x4, y4, K
  134.  
  135.  

BTW, the new trick for getting clear of mousebutton click with oldMouse variable, Steve's trick, is working great and Thanks to Petr and Fellippe for reporting _HYPOT() that's got to be faster than soft-coded distance calculations.


FillEillipse not working.PNG
* FillEillipse not working.PNG (Filesize: 9.71 KB, Dimensions: 808x628, Views: 158)
FillEllipse working.PNG
* FillEllipse working.PNG (Filesize: 9.66 KB, Dimensions: 802x628, Views: 175)
« Last Edit: December 15, 2019, 12:56:54 pm by bplus »