types.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. //
  2. // Measurements
  3. //
  4. /**
  5. * By definition one radian is the angle subtended at the centre
  6. * of a circle by an arc that is equal in length to the radius.
  7. */
  8. export type Radians = number & { _brand: "excalimath__radian" };
  9. /**
  10. * An angle measurement of a plane angle in which one full
  11. * rotation is 360 degrees.
  12. */
  13. export type Degrees = number & { _brand: "excalimath_degree" };
  14. //
  15. // Range
  16. //
  17. /**
  18. * A number range which includes the start and end numbers in the range.
  19. */
  20. export type InclusiveRange = [number, number] & { _brand: "excalimath_degree" };
  21. //
  22. // Point
  23. //
  24. /**
  25. * Represents a 2D position in world or canvas space. A
  26. * global coordinate.
  27. */
  28. export type GlobalPoint = [x: number, y: number] & {
  29. _brand: "excalimath__globalpoint";
  30. };
  31. /**
  32. * Represents a 2D position in whatever local space it's
  33. * needed. A local coordinate.
  34. */
  35. export type LocalPoint = [x: number, y: number] & {
  36. _brand: "excalimath__localpoint";
  37. };
  38. // Line
  39. /**
  40. * A line is an infinitely long object with no width, depth, or curvature.
  41. */
  42. export type Line<P extends GlobalPoint | LocalPoint> = [p: P, q: P] & {
  43. _brand: "excalimath_line";
  44. };
  45. /**
  46. * In geometry, a line segment is a part of a straight
  47. * line that is bounded by two distinct end points, and
  48. * contains every point on the line that is between its endpoints.
  49. */
  50. export type LineSegment<P extends GlobalPoint | LocalPoint> = [a: P, b: P] & {
  51. _brand: "excalimath_linesegment";
  52. };
  53. //
  54. // Vector
  55. //
  56. /**
  57. * Represents a 2D vector
  58. */
  59. export type Vector = [u: number, v: number] & {
  60. _brand: "excalimath__vector";
  61. };
  62. // Triangles
  63. /**
  64. * A triangle represented by 3 points
  65. */
  66. export type Triangle<P extends GlobalPoint | LocalPoint> = [
  67. a: P,
  68. b: P,
  69. c: P,
  70. ] & {
  71. _brand: "excalimath__triangle";
  72. };
  73. //
  74. // Polygon
  75. //
  76. /**
  77. * A polygon is a closed shape by connecting the given points
  78. * rectangles and diamonds are modelled by polygons
  79. */
  80. export type Polygon<Point extends GlobalPoint | LocalPoint> = Point[] & {
  81. _brand: "excalimath_polygon";
  82. };
  83. //
  84. // Curve
  85. //
  86. /**
  87. * Cubic bezier curve with four control points
  88. */
  89. export type Curve<Point extends GlobalPoint | LocalPoint> = [
  90. Point,
  91. Point,
  92. Point,
  93. Point,
  94. ] & {
  95. _brand: "excalimath_curve";
  96. };
  97. export type PolarCoords = [
  98. radius: number,
  99. /** angle in radians */
  100. angle: number,
  101. ];
  102. /**
  103. * Angles are in radians and centered on 0, 0. Zero radians on a 1 radius circle
  104. * corresponds to (1, 0) cartesian coordinates (point), i.e. to the "right".
  105. */
  106. export type SymmetricArc = {
  107. radius: number;
  108. startAngle: number;
  109. endAngle: number;
  110. };