Advertisement
Chronos_Ouroboros

Line clipping fail

Mar 22nd, 2018
398
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.21 KB | None | 0 0
  1.     static bool, Vector2, Vector2 LineClipCircle (Vector2 v1, Vector2 v2, Vector2 c, double r) {
  2.         let a = v1;
  3.         let b = v2 - v1;
  4.         let d = a - c;
  5.  
  6.         double quadA = b dot b;
  7.         double quadB = b dot d;
  8.         double quadC = d dot d - r * r;
  9.  
  10.         double discriminant = quadB * quadB - 4 * quadA * quadC;
  11.  
  12.         if (discriminant < 0) {
  13.             return false, v1, v2;
  14.         } else {
  15.             double discSqrt = sqrt (discriminant);
  16.             let lambda0 = (-quadB + discSqrt) / (2 * quadA);
  17.             let lambda1 = (-quadB - discSqrt) / (2 * quadA);
  18.             let interPoint0 = a + lambda0 * b;
  19.             let interPoint1 = a + lambda1 * b;
  20.  
  21.             return true, interPoint0, interPoint1;
  22.         }
  23.     }
  24.  
  25.     [...]
  26.  
  27.             double angleCos = Cos (-pPawn.angle), angleSin = Sin (-pPawn.angle);
  28.             Vector3 pPawnXYPos = (pPawn.prev + (TicFrac * (pPawn.pos - pPawn.prev)));
  29.             Vector2 radarOffs = (radarPos.x - RADAR_RADIUS, radarPos.y + RADAR_RADIUS);
  30.  
  31.             let linesIterator = BlockLinesIterator.Create (CPlayer.mo, RADAR_MAXDIST);
  32.             while (linesIterator.Next ()) {
  33.                 let curLine = linesIterator.CurLine;
  34.  
  35.                 // Coords
  36.                 Vector2 worldPosV0 = (pPawnXYPos.xy - curLine.v1.p) / RADAR_SCALE, screenPosV0 = radarOffs;
  37.                 Vector2 worldPosV1 = (pPawnXYPos.xy - curLine.v2.p) / RADAR_SCALE, screenPosV1 = radarOffs;
  38.  
  39.                 screenPosV0 += (worldPosV0.x * angleSin + worldPosV0.y * angleCos,
  40.                                 worldPosV0.x * angleCos - worldPosV0.y * angleSin);
  41.                 screenPosV1 += (worldPosV1.x * angleSin + worldPosV1.y * angleCos,
  42.                                 worldPosV1.x * angleCos - worldPosV1.y * angleSin);
  43.  
  44.                 bool intersected;
  45.                 [intersected, screenPosV0, screenPosV1] = LineClipCircle (screenPosV0, screenPosV1, radarOffs, RADAR_RADIUS);
  46.  
  47.                 if (!intersected)
  48.                     continue;
  49.  
  50.                 if (screenPosV0 == screenPosV1) // Just in case
  51.                     continue;
  52.  
  53.                 DrawLine (screenPosV0, screenPosV1, Color (256, 256, 256, 0), DI_SCREEN_RIGHT_TOP);
  54.             }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement