summaryrefslogtreecommitdiff
path: root/src/Objects.scala
blob: b4e643569b078f9598751c85c59c06e3e105b8d1 (plain)
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package net.iximeow.raytrace

import java.awt.image.BufferedImage

object Objects {
  /*
   * 3d nah
  case class Point(x: Double, y: Double, z: Double)
  case class Plane(pitch: Double, roll: Double, altitude: Double)
  case class BoundedPlane(pitch: Double, roll: Double, center: Point, h: Point, w: Point)
  */

  case class Point(x: Double, y: Double) {
    def +(other: Point): Point = Point(x + other.x, y + other.y)
    def -(other: Point): Point = Point(x - other.x, y - other.y)
    def /(scale: Double): Point = Point(x / scale, y / scale)
    def *(scale: Double): Point = Point(x * scale, y * scale)
    def magnitude: Double = distTo(Point.Zero)
    def distTo(other: Point) = Math.sqrt(sqDistTo(other))
    def sqDistTo(other: Point) = (other.x - x) * (other.x - x) + (other.y - y) * (other.y - y)
    def dot(other: Point): Double = x * other.x + y * other.y
  }
  object Point {
    val Zero = Point(0, 0)
  }
  case class Line(m: Double, b: Double)
  object Line {
    def fromPoints(p1: Point, p2: Point): Line = {
      val m = (p2.y - p1.y) / (p2.x - p1.x)
      val b = p1.y - m*p1.x
      Line(m, b)
    }
  }
  /*
   * Segments are defined for t in [0, 1]
   */
  case class Segment(x: Double, y: Double, initial: Point) {
    def at(t: Double): Point = Point(x * t, y * t) + initial
    def apply = at _
    def length = Math.sqrt(x * x + y * y)
    def intersect(other: Segment): Double = {
      /*
       *  P1 = ai + t * a
       *  P2 = bi + u * b
       *  P1 == P2, so
       *  ai + t * a = bi + u * b
       *  ...
       *  ai.x + t * a.x = bi.x + u * b.x
       *  ai.y + t * a.y = bi.y + u * b.y
       *  t = (bi.x + u * b.x - ai.x) / a.x
       *  ai.y + a.y * (bi.x + u * b.x - ai.x) / a.x = bi.y + u * b.y
       *  ai.y + a.y * bi.x / a.x + u * b.x * a.y / a.x - ai.x * a.y / a.x = bi.y + u * b.y
       *  ai.y - bi.y + (a.y / a.x) (bi.x - ai.x) = u * b.y - u * b.x * a.y / a.x
       *  ai.y - bi.y + (a.y / a.x) (bi.x - ai.x) / (b.y - b.x * a.y / a.x) = u
       *
       *  (a.x * (ai.y - bi.y) + a.y * (bi.x - ai.x)) / (b.y - b.x * a.y)
       */
      val u = (
        x * (initial.y - other.initial.y) +
        y * (other.initial.x - initial.x)
      ) / (
        other.y * x - other.x * y
      )

      u
    }

    def intersectChecked(other: Segment): Option[Point] = {
      val u = intersect(other)
      if (u >= 0 && u <= 1) {
        //println("Intersection is at u=" + u)

        Some(other.at(u))
      } else {
        None
      }
    }
    def tFor(p: Point): Option[Double] = {
      (x, y) match {
        case (0, _) => Some((p.y - initial.y) / y)
        case (_, 0) => Some((p.x - initial.x) / x)
        case (_, _) => {
          val xT = (p.x - initial.x) / x
          val yT = (p.y - initial.y) / y
          if (Math.abs(xT - yT) < 0.000001) {
            Some(xT)
          } else {
            None
          }
        }
      }
    }
    def rotate(angle: Double, about: Point = Point(0, 0)): Segment = {
      val start = this.at(0)
      val end = this.at(1)
      val newStart = {
        val offset = start - about
        val m = offset.magnitude
        val newAngle = Math.atan2(offset.y, offset.x) + angle
        Point(Math.cos(newAngle) * m, Math.sin(newAngle) * m) + about
      }
      val newEnd = {
        val offset = end - about
        val m = offset.magnitude
        val newAngle = Math.atan2(offset.y, offset.x) + angle
        Point(Math.cos(newAngle) * m, Math.sin(newAngle) * m) + about
      }
      Segment.fromPoints(newStart, newEnd)
    }
    def renderTo(buf: BufferedImage, scale: Double = 1, xoff: Int = 0, yoff: Int = 0, color: Int = 0x808000): Unit = {
      try {
        for (i <- (0 to 100)) {
          val point = this.at(i / 100.0)
          buf.setRGB(Math.round((point.x * scale).toFloat) + xoff, Math.round((point.y * scale).toFloat) + yoff, color)
        }
      } catch {
        case (x: ArrayIndexOutOfBoundsException) => { /* well, we're not properly rendering a region so uh just ignore the failure i guess lol */ }
      }
    }
    def normal: Segment = {
      val normalMag = Math.sqrt(x * x + y * y)
      val finalNormMult = 1.5 / normalMag
      Ray(-y * finalNormMult, x * finalNormMult, (at(0) + at(1)) / 2).toSegment
    }
  }
  object Segment {
    def fromPoints(p1: Point, p2: Point): Segment = {
      val (x0, y0) = (p2.x - p1.x, p2.y - p1.y)
      Segment(x0, y0, p1)
    }
    def fromPointWithAngle(p: Point, angle: Double): Segment = {
      val (x0, y0) = (Math.cos(angle), Math.sin(angle))
      Segment(x0, y0, p)
    }
  }
  case class Ray(x: Double, y: Double, initial: Point) {
    def toSegment: Segment = Segment(x, y, initial)
    def endingAt(p: Point): Ray = {
      val intersectAt = this.toSegment.tFor(p)
      intersectAt.map(t =>
        Ray(x * t, y * t, initial)
      ).getOrElse(this)
    }
    def dot(other: Ray): Double = {
      x * other.x + y * other.y
    }
    def mag: Double = {
      Math.sqrt(x * x + y * y)
    }
  }
}