All files / src/shared/lib/helpers slope.ts

0% Statements 0/33
0% Branches 0/13
0% Functions 0/1
0% Lines 0/32

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66                                                                                                                                   
import type { SlopeIncline } from "store/features/editor/editorState";
import { areRelativelyEqual } from "./math";
 
export const calculateSlope = (
  startX: number,
  startY: number,
  endX: number,
  endY: number,
  slopeDirectionHorizontal: "left" | "right",
  slopeDirectionVertical: "left" | "right",
  wallMode: boolean
) => {
  let newEndX = endX;
  let newEndY = endY;
  let slopeIncline: SlopeIncline = "medium";
 
  const diffX = newEndX - startX;
  const diffY = newEndY - startY;
  const signX = Math.sign(diffX);
  let signY = Math.sign(diffY);
 
  if (wallMode) {
    if (Math.abs(diffY) > Math.abs(diffX)) {
      // Vertical
      newEndX = startX;
    } else {
      // Horizontal
      newEndY = startY;
    }
  } else if (areRelativelyEqual(Math.abs(diffX), Math.abs(diffY), 0.4)) {
    // drawing 45 degree line
    const length = Math.max(Math.abs(diffX), Math.abs(diffY));
    newEndX = startX + Math.sign(diffX) * length;
    newEndY = startY + Math.sign(diffY) * length;
    slopeIncline = "medium";
  } else {
    if (Math.abs(diffY) > Math.abs(diffX)) {
      // Steep slope - Create 45 deg for now
      const length = Math.max(Math.abs(diffX), Math.abs(diffY));
      newEndX = startX + (Math.sign(diffX) || 1) * length;
      newEndY = startY + Math.sign(diffY) * length;
      slopeIncline = "medium";
    } else {
      // Shallow slope
      slopeIncline = "shallow";
      const length = Math.max(Math.abs(diffX), Math.abs(diffY));
 
      Iif (startY === endY) {
        if (slopeDirectionHorizontal === "left") {
          signY = 1;
        } else {
          signY = -1;
        }
      }
      newEndX = startX + signX * length;
      newEndY = startY + signY * length * 0.5;
    }
  }
 
  return {
    endX: newEndX,
    endY: newEndY,
    slopeIncline,
  };
};