Sorceress

Untitled

Jul 12th, 2017
375
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. TYPE vec2
  2.   x AS SINGLE
  3.   y AS SINGLE
  4. END TYPE
  5. DIM p(255) AS vec2
  6. DIM v(255) AS vec2
  7. DIM pmax AS INTEGER
  8. DIM Q AS vec2
  9. CONST pi = 3.141592
  10.  
  11. Q.x = -4: Q.y = 2 'Your point goes here
  12.  
  13. 'read in the polygon
  14. READ pmax
  15. FOR i = 0 TO pmax - 1
  16.   READ p(i).x, p(i).y
  17. NEXT
  18.  
  19. DO
  20.   KEY$ = INKEY$
  21.   IF KEY$ = CHR$(27) THEN EXIT DO
  22.   SELECT CASE MID$(KEY$, 2)
  23.     CASE "H": Q.y = Q.y - .1
  24.     CASE "P": Q.y = Q.y + .1
  25.     CASE "K": Q.x = Q.x - .1
  26.     CASE "M": Q.x = Q.x + .1
  27.   END SELECT
  28.  
  29.  
  30.   'compute vectors from Q
  31.   FOR i = 0 TO pmax - 1
  32.     v(i).x = p(i).x - Q.x
  33.     v(i).y = p(i).y - Q.y
  34.   NEXT
  35.  
  36.   'draw
  37.   SCREEN 12: CLS
  38.   COLOR 13: PRINT "Point in Polygon Demo (c)2017, @_sorceress"
  39.   COLOR 15: PRINT "Use arrow keys to move the point"
  40.   FOR i = 0 TO pmax
  41.     IF i = pmax THEN j = 0 ELSE j = i
  42.     x = p(j).x: y = p(j).y: GOSUB project
  43.     IF i = 0 THEN PSET (px, py), 10 ELSE LINE -(px, py), 10
  44.   NEXT
  45.   x = Q.x: y = Q.y: GOSUB project
  46.   CIRCLE (px, py), 5, 13
  47.  
  48.   'Compute inside/outside
  49.   sum = 0
  50.   FOR i = 0 TO pmax - 1
  51.     IF i > 0 THEN j = i - 1 ELSE j = pmax - 1
  52.     a = v(i).x * v(j).y - v(i).y * v(j).x
  53.     b = v(i).x * v(j).x + v(i).y * v(j).y
  54.     sum = sum + _ATAN2(a, b)
  55.   NEXT
  56.   COLOR 11: PRINT "Point is at: "; Q.x; Q.y
  57.   IF ABS(sum) < pi THEN
  58.     COLOR 12: PRINT "Point is OUTSIDE the polygon."
  59.   ELSE
  60.     COLOR 10: PRINT "Point is INSIDE the polygon."
  61.   END IF
  62.   COLOR 7: PRINT "Sum of angles = "; sum
  63.   _DISPLAY
  64.   SLEEP 0
  65.  
  66. LOOP
  67. SYSTEM
  68.  
  69. project:
  70. px = 320 + 20 * x: py = 200 + 20 * y
  71. RETURN
  72.  
  73. 'Your polygon goes here
  74. DATA 9
  75. DATA -5,0
  76. DATA 0,5
  77. DATA 0,1
  78. DATA 5,5
  79. DATA 2,-5
  80. DATA 0,-5
  81. DATA -1,-1
  82. DATA -2,-5
  83. DATA -5,-5
RAW Paste Data