Author Topic: _MAPTRIANGLE Distortion  (Read 4278 times)

0 Members and 1 Guest are viewing this topic.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
_MAPTRIANGLE Distortion
« on: August 02, 2018, 03:28:49 pm »
Hi, in following code I think I am using the exact same dimensions and coordinates to map a cheese section to the shooter (when it's angle is zero) such that when you press a button the wedge shooter should appear cut directly from the cheese but the mapping is distorted the y radius of holes looks OK but the x dimension of holes are really stretched? I can't even tell if the image is coming from part of the corresponding wedge outline made in the cheese...

Maybe some fresh eyes can see the blunder I am making. I know I best not call it a bug, yet...
Code: QB64: [Select]
  1. _TITLE "Cheese Factory 2"
  2. 'QB64 X 64 version 1.2 20180228/86  from git b301f92
  3.  
  4. '2018-08-01 started from idea I had while trying to fix holes in Colbalt's cheese wedge shooter
  5. '2018-08-02 Make growCheese sub completely self contained
  6. ' fix shooter:
  7. '1. shooterX, shooterY must be in center of wedge
  8. '2. wedge needs rounded back as if cut from round cheese cake
  9.  
  10. CONST wW = 800
  11. CONST wH = 600
  12. CONST sR = 100
  13.  
  14. SCREEN _NEWIMAGE(wW, wH, 32)
  15. _SCREENMOVE 360, 60
  16. DIM SHARED shooterX, shooterY, shooterA
  17. shooterX = 400: shooterY = 300: shooterA = 0
  18.  
  19. DIM SHARED cheese&
  20. growCheese
  21. _PUTIMAGE , cheese&
  22.  
  23. 'OK on the cheese here is outline of where the shooter wedge will be mapped
  24. COLOR _RGB32(0, 0, 0)
  25. CIRCLE (wW / 2, wH / 2), sR + 1
  26. x = shooterX + sR * COS(shooterA)
  27. y = shooterY + sR * SIN(shooterA)
  28. x1 = shooterX + sR * COS(shooterA + _PI(5 / 6))
  29. y1 = shooterY + sR * SIN(shooterA + _PI(5 / 6))
  30. x2 = shooterX + sR * COS(shooterA + _PI(7 / 6))
  31. y2 = shooterY + sR * SIN(shooterA + _PI(7 / 6))
  32. LINE (x, y)-(x1, y1)
  33. LINE (x1, y1)-(x2, y2)
  34. LINE (x2, y2)-(x, y)
  35.  
  36. COLOR _RGB32(255, 255, 0)
  37. _PRINTSTRING (20, 20), "  Here is a sample of the cheese from which we are taking a wedge,  "
  38. _PRINTSTRING (20, 60), "  press any key to transfer cheese image to shooter surrounded by black square.  "
  39. _PRINTSTRING (20, 100), "  WHY IS THE IMAGE DISTORTED? a 1 to 1 ratio was used in _MAPTRIANGLE to same dimensions.  "
  40. _PRINTSTRING (20, 140), "  press left and right arrows to swing shooter around... escape quits.  "
  41.  
  42.     IF _KEYDOWN(19200) THEN _DELAY .05: shooterA = shooterA - _PI(5 / 360)
  43.     IF _KEYDOWN(19712) THEN _DELAY .05: shooterA = shooterA + _PI(5 / 360)
  44.     LINE (wW / 2 - sR - 5, wH / 2 - sR - 5)-STEP(2 * sR + 10, 2 * sR + 10), _RGB32(0, 0, 0), BF
  45.     drawShooter
  46.     _DISPLAY
  47.  
  48. SUB drawShooter ()
  49.     'first makeCheese and use cheese& image to build shooter image
  50.     'dim shared ShooterX, ShooterY, ShooterA, cheese&
  51.     x = shooterX + sR * COS(shooterA)
  52.     y = shooterY + sR * SIN(shooterA)
  53.     x1 = shooterX + sR * COS(shooterA + _PI(5 / 6))
  54.     y1 = shooterY + sR * SIN(shooterA + _PI(5 / 6))
  55.     cx = wW / 2 + sR * COS(0)
  56.     cy = wH / 2 + sR * SIN(0)
  57.     cx1 = cx + sR * COS(_PI(5 / 6))
  58.     cy1 = cy + sR * SIN(_PI(5 / 6))
  59.  
  60.     'take small traingles off cheese& image  and map them onto main screen at shooter position
  61.     stepper = _PI(1 / 72)
  62.     starter = _PI(5 / 6) + stepper
  63.     stopper = _PI(7 / 6)
  64.     FOR a = starter TO stopper STEP stepper
  65.         'one to one ratio of mapping
  66.         x2 = shooterX + sR * COS(shooterA + a)
  67.         y2 = shooterY + sR * SIN(shooterA + a)
  68.         cx2 = cx + sR * COS(a)
  69.         cy2 = cy + sR * SIN(a)
  70.         _MAPTRIANGLE (cx, cy)-(cx1, cy1)-(cx2, cy2), cheese& TO(x, y)-(x1, y1)-(x2, y2)
  71.         x1 = x2: y1 = y2: cx1 = cx2: cy1 = cy2
  72.     NEXT
  73.  
  74.     'check overlap:  not perfect but good enough!
  75.     x = shooterX + sR * COS(shooterA)
  76.     y = shooterY + sR * SIN(shooterA)
  77.     x1 = shooterX + sR * COS(shooterA + _PI(5 / 6))
  78.     y1 = shooterY + sR * SIN(shooterA + _PI(5 / 6))
  79.     x2 = shooterX + sR * COS(shooterA + _PI(7 / 6))
  80.     y2 = shooterY + sR * SIN(shooterA + _PI(7 / 6))
  81.     LINE (x, y)-(x1, y1), _RGB32(255, 255, 255)
  82.     LINE (x1, y1)-(x2, y2), _RGB32(255, 255, 255)
  83.     LINE (x2, y2)-(x, y), _RGB32(255, 255, 255)
  84.  
  85.  
  86.  
  87. SUB growCheese () 'make this more self contained than first version, all hole stuff just in here
  88.     curr& = _DEST
  89.     IF cheese& THEN _FREEIMAGE cheese&
  90.     cheese& = _NEWIMAGE(wW, wH, 32)
  91.     _DEST cheese&
  92.     nHoles = 300: maxHoleLife = 20: maxHoleRadius = 7: tfStart = 1
  93.     DIM hx(nHoles), hy(nHoles), hLife(nHoles)
  94.     FOR i = 1 TO nHoles
  95.         GOSUB newHole
  96.     NEXT
  97.  
  98.     tfStart = 0
  99.     FOR layr = 1 TO 30
  100.         LINE (0, 0)-(wW, wH), _RGBA32(255, 255, 0, 50), BF 'layer of cheese
  101.         FOR i = 1 TO nHoles 'holes in layer
  102.             IF hLife(i) + 1 > maxHoleLife THEN GOSUB newHole ELSE hLife(i) = hLife(i) + 1
  103.             hx(i) = hx(i) + RND * 2 - 1
  104.             hy(i) = hy(i) + RND * 2 - 1
  105.             IF hLife(i) < maxHoleRadius THEN
  106.                 radius = hLife(i)
  107.             ELSEIF maxHoleLife - hLife(i) < maxHoleRadius THEN
  108.                 radius = maxHoleLife - hLife(i)
  109.             ELSE
  110.                 radius = maxHoleRadius
  111.             END IF
  112.             COLOR _RGBA32(0, 0, 0, 50)
  113.             fcirc hx(i), hy(i), radius
  114.         NEXT
  115.     NEXT
  116.  
  117.  
  118.     _DEST curr&
  119.     EXIT SUB
  120.  
  121.     newHole:
  122.     hx(i) = wW * RND
  123.     hy(i) = wH * RND
  124.     IF tfStart THEN hLife(i) = INT(RND * maxHoleLife) ELSE hLife(i) = 1
  125.     RETURN
  126.  
  127.  
  128. 'Steve McNeil's  copied from his forum   note: Radius is too common a name
  129. SUB fcirc (CX AS LONG, CY AS LONG, R AS LONG)
  130.     DIM subRadius AS LONG, RadiusError AS LONG
  131.     DIM X AS LONG, Y AS LONG
  132.  
  133.     subRadius = ABS(R)
  134.     RadiusError = -subRadius
  135.     X = subRadius
  136.     Y = 0
  137.  
  138.     IF subRadius = 0 THEN PSET (CX, CY): EXIT SUB
  139.  
  140.     ' Draw the middle span here so we don't draw it twice in the main loop,
  141.     ' which would be a problem with blending turned on.
  142.     LINE (CX - X, CY)-(CX + X, CY), , BF
  143.  
  144.     WHILE X > Y
  145.         RadiusError = RadiusError + Y * 2 + 1
  146.         IF RadiusError >= 0 THEN
  147.             IF X <> Y + 1 THEN
  148.                 LINE (CX - Y, CY - X)-(CX + Y, CY - X), , BF
  149.                 LINE (CX - Y, CY + X)-(CX + Y, CY + X), , BF
  150.             END IF
  151.             X = X - 1
  152.             RadiusError = RadiusError - X * 2
  153.         END IF
  154.         Y = Y + 1
  155.         LINE (CX - X, CY - Y)-(CX + X, CY - Y), , BF
  156.         LINE (CX - X, CY + Y)-(CX + X, CY + Y), , BF
  157.     WEND
  158.  
« Last Edit: August 02, 2018, 03:34:46 pm by bplus »

Marked as best answer by bplus on August 02, 2018, 03:03:00 pm

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: _MAPTRIANGLE Distortion
« Reply #1 on: August 02, 2018, 05:02:49 pm »
Hi Bplus. I test it, you map not the same areas. If i understand it correctly, then muss be values for cx, cy the same as for x, y and  cx1, cy1 the same as for x1, y1  and cx2, cy2 the same as for x2, y2 in begin - angle 0, but are not the same.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: _MAPTRIANGLE Distortion
« Reply #2 on: August 02, 2018, 05:20:33 pm »
Hi Petr,

Right my x1, cx1 and x2, cx2 are off by 100! Now to track down why?

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: _MAPTRIANGLE Distortion (Fixed)
« Reply #3 on: August 02, 2018, 05:59:36 pm »
Fixed, I was using the wrong number for the center of the cheese from which I was mapping.

Thanks Petr, for getting me started on checking actual numbers.

So it was my blunder, a shocker! ;D

Code: QB64: [Select]
  1. _TITLE "Cheese Factory 2"
  2. 'QB64 X 64 version 1.2 20180228/86  from git b301f92
  3.  
  4. '2018-08-01 started from idea I had while trying to fix holes in Colbalt's cheese wedge shooter
  5. '2018-08-02 Make growCheese sub completely self contained
  6. ' fix shooter:
  7. '1. shooterX, shooterY must be in center of wedge
  8. '2. wedge needs rounded back as if cut from round cheese cake
  9.  
  10. CONST wW = 800
  11. CONST wH = 600
  12. CONST sR = 100
  13.  
  14. SCREEN _NEWIMAGE(wW, wH, 32)
  15. _SCREENMOVE 360, 60
  16. DIM SHARED shooterX, shooterY, shooterA
  17. shooterX = 400: shooterY = 300
  18.  
  19. DIM SHARED cheese&
  20.  
  21.  
  22. restart:
  23. growCheese
  24. _PUTIMAGE , cheese&
  25. shooterA = 0
  26. 'OK on the cheese here is outline of where the shooter wedge will be mapped
  27. COLOR _RGB32(0, 0, 0)
  28. CIRCLE (wW / 2, wH / 2), sR + 1
  29. x = shooterX + sR * COS(shooterA)
  30. y = shooterY + sR * SIN(shooterA)
  31. x1 = shooterX + sR * COS(shooterA + _PI(5 / 6))
  32. y1 = shooterY + sR * SIN(shooterA + _PI(5 / 6))
  33. x2 = shooterX + sR * COS(shooterA + _PI(7 / 6))
  34. y2 = shooterY + sR * SIN(shooterA + _PI(7 / 6))
  35. LINE (x, y)-(x1, y1)
  36. LINE (x1, y1)-(x2, y2)
  37. LINE (x2, y2)-(x, y)
  38.  
  39. COLOR _RGB32(255, 255, 0)
  40. _PRINTSTRING (20, 20), "  Here is a sample of the cheese from which we are taking a wedge,  "
  41. _PRINTSTRING (20, 60), "  press any key (except spacebar) to transfer cheese image to shooter surrounded by black square.  "
  42. _PRINTSTRING (20, 100), "  press spacebar for another cheesey layout and wedge cut..  "
  43. _PRINTSTRING (20, 140), "  press left and right arrows to swing shooter around... escape quits.  "
  44.  
  45.  
  46. 'show different patterns of holes
  47.     IF _KEYDOWN(32) THEN GOTO restart
  48.     IF _KEYDOWN(19200) THEN _DELAY .05: shooterA = shooterA - _PI(5 / 360)
  49.     IF _KEYDOWN(19712) THEN _DELAY .05: shooterA = shooterA + _PI(5 / 360)
  50.     LINE (wW / 2 - sR - 5, wH / 2 - sR - 5)-STEP(2 * sR + 10, 2 * sR + 10), _RGB32(0, 0, 0), BF
  51.     drawShooter
  52.     _DISPLAY
  53.  
  54. SUB drawShooter ()
  55.     'first makeCheese and use cheese& image to build shooter image
  56.     'dim shared ShooterX, ShooterY, ShooterA, cheese&
  57.  
  58.     cx0 = wW / 2: cy0 = wH / 2 'fix center for cheese wedge
  59.  
  60.     x = shooterX + sR * COS(shooterA)
  61.     y = shooterY + sR * SIN(shooterA)
  62.     x1 = shooterX + sR * COS(shooterA + _PI(5 / 6))
  63.     y1 = shooterY + sR * SIN(shooterA + _PI(5 / 6))
  64.     cx = cx0 + sR * COS(0)
  65.     cy = cy0 + sR * SIN(0)
  66.     cx1 = cx0 + sR * COS(_PI(5 / 6))
  67.     cy1 = cy0 + sR * SIN(_PI(5 / 6))
  68.  
  69.     'take small traingles off cheese& image  and map them onto main screen at shooter position
  70.     stepper = _PI(1 / 72)
  71.     starter = _PI(5 / 6) + stepper
  72.     stopper = _PI(7 / 6)
  73.     FOR a = starter TO stopper STEP stepper
  74.         'one to one ratio of mapping
  75.         x2 = shooterX + sR * COS(shooterA + a)
  76.         y2 = shooterY + sR * SIN(shooterA + a)
  77.         cx2 = cx0 + sR * COS(a)
  78.         cy2 = cy0 + sR * SIN(a)
  79.         _MAPTRIANGLE (cx, cy)-(cx1, cy1)-(cx2, cy2), cheese& TO(x, y)-(x1, y1)-(x2, y2)
  80.  
  81.         'OK check value matches
  82.         IF a = starter THEN
  83.             LOCATE 30, 1: PRINT shooterX, shooterY, COS(shooterA), SIN(shooterA)
  84.             LOCATE 31, 1: PRINT x, y, cx, cy
  85.             LOCATE 32, 1: PRINT x1, y1, cx1, cy1
  86.             LOCATE 33, 1: PRINT x2, y2, cx2, cy2
  87.         END IF
  88.  
  89.         x1 = x2: y1 = y2: cx1 = cx2: cy1 = cy2
  90.     NEXT
  91.  
  92.     'check overlap:  not perfect but good enough!
  93.     x = shooterX + sR * COS(shooterA)
  94.     y = shooterY + sR * SIN(shooterA)
  95.     x1 = shooterX + sR * COS(shooterA + _PI(5 / 6))
  96.     y1 = shooterY + sR * SIN(shooterA + _PI(5 / 6))
  97.     x2 = shooterX + sR * COS(shooterA + _PI(7 / 6))
  98.     y2 = shooterY + sR * SIN(shooterA + _PI(7 / 6))
  99.     LINE (x, y)-(x1, y1), _RGB32(255, 255, 255)
  100.     LINE (x1, y1)-(x2, y2), _RGB32(255, 255, 255)
  101.     LINE (x2, y2)-(x, y), _RGB32(255, 255, 255)
  102.  
  103.  
  104.  
  105. SUB growCheese () 'make this more self contained than first version, all hole stuff just in here
  106.     curr& = _DEST
  107.     IF cheese& THEN _FREEIMAGE cheese&
  108.     cheese& = _NEWIMAGE(wW, wH, 32)
  109.     _DEST cheese&
  110.     nHoles = 300: maxHoleLife = 20: maxHoleRadius = 7: tfStart = 1
  111.     DIM hx(nHoles), hy(nHoles), hLife(nHoles)
  112.     FOR i = 1 TO nHoles
  113.         GOSUB newHole
  114.     NEXT
  115.  
  116.     tfStart = 0
  117.     FOR layr = 1 TO 30
  118.         LINE (0, 0)-(wW, wH), _RGBA32(255, 255, 0, 50), BF 'layer of cheese
  119.         FOR i = 1 TO nHoles 'holes in layer
  120.             IF hLife(i) + 1 > maxHoleLife THEN GOSUB newHole ELSE hLife(i) = hLife(i) + 1
  121.             hx(i) = hx(i) + RND * 2 - 1
  122.             hy(i) = hy(i) + RND * 2 - 1
  123.             IF hLife(i) < maxHoleRadius THEN
  124.                 radius = hLife(i)
  125.             ELSEIF maxHoleLife - hLife(i) < maxHoleRadius THEN
  126.                 radius = maxHoleLife - hLife(i)
  127.             ELSE
  128.                 radius = maxHoleRadius
  129.             END IF
  130.             COLOR _RGBA32(0, 0, 0, 50)
  131.             fcirc hx(i), hy(i), radius
  132.         NEXT
  133.     NEXT
  134.  
  135.  
  136.     _DEST curr&
  137.     EXIT SUB
  138.  
  139.     newHole:
  140.     hx(i) = wW * RND
  141.     hy(i) = wH * RND
  142.     IF tfStart THEN hLife(i) = INT(RND * maxHoleLife) ELSE hLife(i) = 1
  143.     RETURN
  144.  
  145.  
  146. 'Steve McNeil's  copied from his forum   note: Radius is too common a name
  147. SUB fcirc (CX AS LONG, CY AS LONG, R AS LONG)
  148.     DIM subRadius AS LONG, RadiusError AS LONG
  149.     DIM X AS LONG, Y AS LONG
  150.  
  151.     subRadius = ABS(R)
  152.     RadiusError = -subRadius
  153.     X = subRadius
  154.     Y = 0
  155.  
  156.     IF subRadius = 0 THEN PSET (CX, CY): EXIT SUB
  157.  
  158.     ' Draw the middle span here so we don't draw it twice in the main loop,
  159.     ' which would be a problem with blending turned on.
  160.     LINE (CX - X, CY)-(CX + X, CY), , BF
  161.  
  162.     WHILE X > Y
  163.         RadiusError = RadiusError + Y * 2 + 1
  164.         IF RadiusError >= 0 THEN
  165.             IF X <> Y + 1 THEN
  166.                 LINE (CX - Y, CY - X)-(CX + Y, CY - X), , BF
  167.                 LINE (CX - Y, CY + X)-(CX + Y, CY + X), , BF
  168.             END IF
  169.             X = X - 1
  170.             RadiusError = RadiusError - X * 2
  171.         END IF
  172.         Y = Y + 1
  173.         LINE (CX - X, CY - Y)-(CX + X, CY - Y), , BF
  174.         LINE (CX - X, CY + Y)-(CX + X, CY + Y), , BF
  175.     WEND
  176.  
« Last Edit: August 02, 2018, 06:10:21 pm by bplus »