Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                #output
              
            
!

CSS

              
                #output
  font-family: monospace
              
            
!

JS

              
                # https://github.com/hornairs/blog/blob/master/assets/coffeescripts/flocking/vector.coffee
class Vector
  # Class methods for nondestructively operating
  for name in ['add', 'subtract', 'multiply', 'divide']
    do (name) ->
      Vector[name] = (a, b, useZ) ->
        a.copy()[name](b, useZ)

  isVector: true

  constructor: (@x=0, @y=0, @z=0) ->

  copy: ->
    new Vector(@x, @y, @z)

  magnitude: (useZ) ->
    sum = @x * @x + @y * @y
    sum += @z * @z if useZ
    Math.sqrt sum

  magnitudeSquared: (useZ) ->
    sum = @x * @x + @y * @y
    sum += @z * @z if useZ
    sum

  normalize: (useZ) ->
    m = @magnitude useZ
    @divide m, useZ if m > 0
    @

  limit: (max) ->
    if @magnitude() > max
      @normalize()
      return @multiply(max)
    else
      @

  heading: ->
    -1 * Math.atan2(-1 * @y, @x)

  distance: (other, useZ) ->
    dx = @x - other.x
    dy = @y - other.y
    sum = dx * dx + dy * dy
    if useZ
      dz = @z - other.z
      sum += dz * dz
    Math.sqrt sum

  distanceSquared: (other, useZ) ->
    dx = @x - other.x
    dy = @y - other.y
    sum = dx * dx + dy * dy
    if useZ
      dz = @z - other.z
      sum += dz * dz
    sum

  subtract: (other, useZ) ->
    @x -= other.x
    @y -= other.y
    @z -= other.z if useZ
    @

  add: (other, useZ) ->
    @x += other.x
    @y += other.y
    @z += other.z if useZ
    @

  divide: (n, useZ) ->
    [@x, @y] = [@x / n, @y / n]
    @z = @z / n if useZ
    @

  multiply: (n, useZ) ->
    [@x, @y] = [@x * n, @y * n]
    @z = @z * n if useZ
    @

  dot: (other, useZ) ->
    sum = @x * other.x + @y * other.y
    sum += @z + other.z if useZ
    sum

  # Not the strict projection, the other isn't converted to a unit vector first.
  projectOnto: (other, useZ) ->
    other.copy().multiply(@dot(other, useZ), useZ)

  isZero: (useZ) ->
    result = @x is 0 and @y is 0
    result = result and @z is 0 if useZ
    result

  equals: (other, useZ) ->
    result = other and @x is other.x and @y is other.y
    result = result and @z is other.z if useZ
    result

  # Rotate it around the origin
  # If we ever want to make this also use z: https://en.wikipedia.org/wiki/Axes_conventions
  rotate: (theta) ->
    return @ unless theta
    [@x, @y] = [Math.cos(theta) * @x - Math.sin(theta) * @y, Math.sin(theta) * @x + Math.cos(theta) * @y]
    @

  invalid: () ->
    return (@x is Infinity) || isNaN(@x) || @y is Infinity || isNaN(@y) || @z is Infinity || isNaN(@z)

  toString: (useZ) ->
    useZ = true
    return "{x: #{@x.toFixed(2)}, y: #{@y.toFixed(2)}, z: #{@z.toFixed(2)}}" if useZ
    return "{x: #{@x.toFixed(0)}, y: #{@y.toFixed(0)}}"


class Line
  EPS = 1e-9
  
  constructFromParams: (@a, @b, @c) ->
    @
    
  constructFromPoints: (x1, y1, x2, y2) ->
    @.b = x1 - x2
    @.a = y2 - y1
    @.c = -@.a * x1 - @.b * y1
    @
    
  _det: (a, b, c, d) ->
    a * d - b * c
  
  intersect: (other) ->
    zn = @._det(@.a, @.b, other.a, other.b)
    if (Math.abs(zn) < EPS)
      return false;
    new Vector(- @._det(@.c, @.b, other.c, other.b) / zn, - @._det(@.a, @.c, other.a, other.c) / zn)
    
  parallel: (other) ->
    Math.abs(det(@.a, @.b, other.a, other.b)) < EPS

class Segment
  EPS = 1e-9
  constructor: (@startPoint, @endPoint) ->
    
  distanceToPoint: (point) -> 
    res = Math.min(point.distance(@.startPoint), point.distance(@.endPoint))
    Line line = new Line().constructFromPoints(@startPoint.x, @startPoint.y, @endPoint.x, @endPoint.y)
    a = -line.b
    b = line.a
    c = -a * point.x - b * point.y
    Line normal = new Line().constructFromParams(a, b, c)
    intersect = normal.intersect(line)
    if Math.abs(@startPoint.distance(@endPoint) - @startPoint.distance(intersect) - @endPoint.distance(intersect)) < EPS
      res = Math.min(res, point.distance(intersect))
    res
    
  
    
class Rectangle
  @className: "Rectangle"
  # Class methods for nondestructively operating
  for name in ['add', 'subtract', 'multiply', 'divide']
    do (name) ->
      Rectangle[name] = (a, b) ->
        a.copy()[name](b)

  constructor: (@x=0, @y=0, @width=0, @height=0, @rotation=0) ->

  copy: ->
    new Rectangle(@x, @y, @width, @height, @rotation)

  getPos: ->
    new Vector(@x, @y)

  vertices: ->
    # Counter-clockwise, starting from bottom left (when unrotated)
    [w2, h2, cos, sin] = [@width / 2, @height / 2, Math.cos(@rotation), Math.sin(-@rotation)]
    [
      new Vector @x - (w2 * cos - h2 * sin), @y - (w2 * sin + h2 * cos)
      new Vector @x - (w2 * cos + h2 * sin), @y - (w2 * sin - h2 * cos)
      new Vector @x + (w2 * cos - h2 * sin), @y + (w2 * sin + h2 * cos)
      new Vector @x + (w2 * cos + h2 * sin), @y + (w2 * sin - h2 * cos)
    ]

  lineSegments: ->
    vertices = @vertices()
    lineSegment0 = new LineSegment(vertices[0], vertices[1])
    lineSegment1 = new LineSegment(vertices[1], vertices[2])
    lineSegment2 = new LineSegment(vertices[2], vertices[3])
    lineSegment3 = new LineSegment(vertices[3], vertices[0])
    [lineSegment0, lineSegment1, lineSegment2, lineSegment3]

  touchesRect: (other) ->
    # Whether this rect shares part of any edge with other rect, for non-rotated, non-overlapping rectangles.
    # I think it says kitty-corner rects touch, but I don't think I want that.
    # Float instability might get me, too.
    [bl1, tl1, tr1, br1] = @vertices()
    [bl2, tl2, tr2, br2] = other.vertices()
    return false if tl1.x > tr2.x or tl2.x > tr1.x
    return false if bl1.y > tl2.y or bl2.y > tl1.y
    return true if tl1.x is tr2.x or tl2.x is tr1.x
    return true if tl1.y is bl2.y or tl2.y is bl1.y
    false

  touchesPoint: (p) ->
    # Whether this rect has point p exactly on one of its edges, assuming no rotation.
    [bl, tl, tr, br] = @vertices()
    return false unless p.y >= bl.y and p.y <= tl.y
    return false unless p.x >= bl.x and p.x <= br.x
    return true if p.x is bl.x or p.x is br.x
    return true if p.y is bl.y or p.y is tl.y
    false

  axisAlignedBoundingBox: (rounded=true) ->
    box = @copy()
    return box unless @rotation
    box.rotation = 0
    [left, top] = [9001, 9001]
    for vertex in @vertices()
      [left, top] = [Math.min(left, vertex.x), Math.min(top, vertex.y)]
    if rounded
      [left, top] = [Math.round(left), Math.round(top)]
    [box.width, box.height] = [2 * (@x - left), 2 * (@y - top)]
    box

  distanceToPoint: (p) ->
    # Get p in rect's coordinate space, then operate in one quadrant
    p = Vector.subtract(p, @getPos()).rotate(-@rotation)
    dx = Math.max(Math.abs(p.x) - @width / 2, 0)
    dy = Math.max(Math.abs(p.y) - @height / 2, 0)
    Math.sqrt dx * dx + dy * dy

  distanceSquaredToPoint: (p) ->
    # Doesn't handle rotation; just supposed to be faster than distanceToPoint
    dx = Math.max(Math.abs(p.x) - @width / 2, 0)
    dy = Math.max(Math.abs(p.y) - @height / 2, 0)
    dx * dx + dy * dy
    
    
  distanceToRectangle: (other) ->
    [firstVertices, secondVertices] = [@.vertices(), other.vertices()]
    [firstEdges, secondEdges] = [[], []]
    for i in [0..3]
      firstEdges.push new Segment(firstVertices[i], firstVertices[(i + 1) % 4])
      secondEdges.push new Segment(secondVertices[i], secondVertices[(i + 1) % 4])
    ans = Infinity
    for i in [0..3]
      for j in [0..firstEdges.length - 1]
        ans = Math.min(ans, firstEdges[j].distanceToPoint(secondVertices[i])) 
      for j in [0..secondEdges.length - 1]
        ans = Math.min(ans, secondEdges[j].distanceToPoint(firstVertices[i]))
    ans

  containsPoint: (p, withRotation=true) ->
    if withRotation and @rotation
      not @distanceToPoint(p)
    else
      @x - @width / 2 < p.x < @x + @width / 2 and @y - @height / 2 < p.y < @y + @height / 2

  intersectsLineSegment: (p1, p2) ->
    [px1, py1, px2, py2] = [p1.x, p1.y, p2.x, p2.y]
    m1 = (py1 - py2) / (px1 - px2)
    b1 = py1 - (m1 * px1)
    vertices = @vertices()
    lineSegments = [[vertices[0], vertices[1]], [vertices[1], vertices[2]], [vertices[2], vertices[3]], [vertices[3], vertices[0]]]
    for lineSegment in lineSegments
      [px1, py1, px2, py2] = [p1.x, p1.y, p2.x, p2.y]
      m2 = (py1 - py2) / (px1 - px2)
      b2 = py1 - (m * px1)
      if m1 != m2
        m = m1 - m2
        b = b2 - b1
        x = b / m
        [littleX, bigX] = if px1 < px2 then [px1, px2] else [px2, px1]
        if x >= littleX and x <= bigX
          y = (m1 * x) + b1
          [littleY, bigY] = if py1 < py2 then [py1, py2] else [py2, py1]
          if littleY <= solution and bigY >= solution
            return true
    false

  intersectsRectangle: (rectangle) ->
    for thisLineSegment in @lineSegments()
      for thatLineSegment in rectangle.lineSegments()
        if thisLineSegment.intersectsLineSegment(thatLineSegment)
          return true
    false

  subtract: (point) ->
    @x -= point.x
    @y -= point.y
    @pos.subtract point
    @

  add: (point) ->
    @x += point.x
    @y += point.y
    @pos.add point
    @

  divide: (n) ->
    [@width, @height] = [@width / n, @height / n]
    @

  multiply: (n) ->
    [@width, @height] = [@width * n, @height * n]
    @

  isEmpty: () ->
    @width == 0 and @height == 0

  invalid: () ->
    return (@x == Infinity) || isNaN(@x) || @y == Infinity || isNaN(@y) || @width == Infinity || isNaN(@width) || @height == Infinity || isNaN(@height) || @rotation == Infinity || isNaN(@rotation)

  toString: ->
    return "{x: #{@x.toFixed(0)}, y: #{@y.toFixed(0)}, w: #{@width.toFixed(0)}, h: #{@height.toFixed(0)}, rot: #{@rotation.toFixed(3)}}"



class Ellipse
  @className: "Ellipse"

  constructor: (@x=0, @y=0, @width=0, @height=0, @rotation=0) ->

  containsPoint: (p, withRotation=true) ->
    [a, b] = [@width / 2, @height / 2]
    [h, k] = [@x, @y]
    [x, y] = [p.x, p.y]
    x2 = Math.pow(x, 2)
    a2 = Math.pow(a, 2)
    a4 = Math.pow(a, 4)
    b2 = Math.pow(b, 2)
    b4 = Math.pow(b, 4)
    h2 = Math.pow(h, 2)
    k2 = Math.pow(k, 2)
    if withRotation and @rotation
      sint = Math.sin(@rotation)
      sin2t = Math.sin(2 * @rotation)
      cost = Math.cos(@rotation)
      cos2t = Math.cos(2 * @rotation)
      numeratorLeft = (-a2 * h * sin2t) + (a2 * k * cos2t) + (a2 * k) + (a2 * x * sin2t)
      numeratorMiddle = Math.SQRT2 * Math.sqrt((a4 * b2 * cos2t) + (a4 * b2) - (a2 * b4 * cos2t) + (a2 * b4) - (2 * a2 * b2 * h2) + (4 * a2 * b2 * h * x) - (2 * a2 * b2 * x2))
      numeratorRight = (b2 * h * sin2t) - (b2 * k * cos2t) + (b2 * k) - (b2 * x * sin2t)
      denominator = (a2 * cos2t) + a2 - (b2 * cos2t) + b2
      solution1 = (numeratorLeft - numeratorMiddle + numeratorRight) / denominator
      solution2 = (numeratorLeft + numeratorMiddle + numeratorRight) / denominator
      if (not isNaN solution1) and (not isNaN solution2)
        [bigSolution, littleSolution] = if solution1 > solution2 then [solution1, solution2] else [solution2, solution1]
        if y > littleSolution and y < bigSolution
          return true
        else
          return false
      else
        return false
    else
      numeratorLeft = a2 * k
      numeratorRight = Math.sqrt((a4 * b2) - (a2 * b2 * h2) + (2 * a2 * b2 * h * x) - (a2 * b2 * x2))
      denominator = a2
      solution1 = (numeratorLeft + numeratorRight) / denominator
      solution2 = (numeratorLeft - numeratorRight) / denominator
      if (not isNaN solution1) and (not isNaN solution2)
        [bigSolution, littleSolution] = if solution1 > solution2 then [solution1, solution2] else [solution2, solution1]
        if y > littleSolution and y < bigSolution
          return true
        else
          return false
      else
        return false
    false

  intersectsLineSegment: (p1, p2) ->
    [px1, py1, px2, py2] = [p1.x, p1.y, p2.x, p2.y]
    m = (py1 - py2) / (px1 - px2)
    m2 = Math.pow(m, 2)
    c = py1 - (m * px1)
    c2 = Math.pow(c, 2)
    [a, b] = [@width / 2, @height / 2]
    [h, k] = [@x, @y]
    a2 = Math.pow(a, 2)
    a4 = Math.pow(a, 2)
    b2 = Math.pow(b, 2)
    b4 = Math.pow(b, 4)
    h2 = Math.pow(h, 2)
    k2 = Math.pow(k, 2)
    sint = Math.sin(@rotation)
    sin2t = Math.sin(2 * @rotation)
    cost = Math.cos(@rotation)
    cos2t = Math.cos(2 * @rotation)
    if (not isNaN m) and m != Infinity and m != -Infinity
      numeratorLeft = (-a2 * c * m * cos2t) - (a2 * c * m) + (a2 * c * sin2t) - (a2 * h * m * sin2t) - (a2 * h * cos2t) + (a2 * h) + (a2 * k * m * cos2t) + (a2 * k * m) - (a2 * k * sin2t)
      numeratorMiddle = Math.SQRT2 * Math.sqrt((a4 * b2 * m2 * cos2t) + (a4 * b2 * m2) - (2 * a4 * b2 * m * sin2t) - (a4 * b2 * cos2t) + (a4 * b2) - (a2 * b4 * m2 * cos2t) + (a2 * b4 * m2) + (2 * a2 * b4 * m * sin2t) + (a2 * b4 * cos2t) + (a2 * b4) - (2 * a2 * b2 * c2) - (4 * a2 * b2 * c * h * m) + (4 * a2 * b2 * c * k) - (2 * a2 * b2 * h2 * m2) + (4 * a2 * b2 * h * k * m) - (2 * a2 * b2 * k2))
      numeratorRight = (b2 * c * m * cos2t) - (b2 * c * m) - (b2 * c * sin2t) + (b2 * h * m * sin2t) + (b2 * h * cos2t) + (b2 * h) - (b2 * k * m * cos2t) + (b2 * k * m) + (b2 * k * sin2t)
      denominator = (a2 * m2 * cos2t) + (a2 * m2) - (2 * a2 * m * sin2t) - (a2 * cos2t) + a2 - (b2 * m2 * cos2t) + (b2 * m2) + (2 * b2 * m * sin2t) + (b2 * cos2t) + b2
      solution1 = (-numeratorLeft - numeratorMiddle + numeratorRight) / denominator
      solution2 = (-numeratorLeft + numeratorMiddle + numeratorRight) / denominator
      if (not isNaN solution1) and (not isNaN solution2)
        [littleX, bigX] = if px1 < px2 then [px1, px2] else [px2, px1]
        if (littleX <= solution1 and bigX >= solution1) or (littleX <= solution2 and bigX >= solution2)
          return true
      if (not isNaN solution1) or (not isNaN solution2)
        solution = if not isNaN solution1 then solution1 else solution2
        [littleX, bigX] = if px1 < px2 then [px1, px2] else [px2, px1]
        if littleX <= solution and bigX >= solution
          return true
      else
        return false
    else
      x = px1
      x2 = Math.pow(x, 2)
      numeratorLeft = (-a2 * h * sin2t) + (a2 * k * cos2t) + (a2 * k) + (a2 * x * sin2t)
      numeratorMiddle = Math.SQRT2 * Math.sqrt((a4 * b2 * cos2t) + (a4 * b2) - (a2 * b4 * cos2t) + (a2 * b4) - (2 * a2 * b2 * h2) + (4 * a2 * b2 * h * x) - (2 * a2 * b2 * x2))
      numeratorRight = (b2 * h * sin2t) - (b2 * k * cos2t) + (b2 * k) - (b2 * x * sin2t)
      denominator = (a2 * cos2t) + a2 - (b2 * cos2t) + b2
      solution1 = (numeratorLeft - numeratorMiddle + numeratorRight) / denominator
      solution2 = (numeratorLeft + numeratorMiddle + numeratorRight) / denominator
      if (not isNaN solution1) or (not isNaN solution2)
        solution = if not isNaN solution1 then solution1 else solution2
        [littleY, bigY] = if py1 < py2 then [py1, py2] else [py2, py1]
        if littleY <= solution and bigY >= solution
          return true
      else
        return false
    false


  toString: ->
    return "{x: #{@x.toFixed(0)}, y: #{@y.toFixed(0)}, w: #{@width.toFixed(0)}, h: #{@height.toFixed(0)}, rot: #{@rotation.toFixed(3)}}"



class LineSegment
  @className: "LineSegment"

  constructor: (@a, @b) ->
    @slope = (@a.y - @b.y) / (@a.x - @b.x)
    @y0 = @a.y - (@slope * @a.x)
    @left = if @a.x < @b.x then @a else @b
    @right = if @a.x > @b.x then @a else @b
    @bottom = if @a.y < @b.y then @a else @b
    @top = if @a.y > @b.y then @a else @b

  y: (x) ->
    (@slope * x) + @y0

  x: (y) ->
    (y - @y0) / @slope

  intersectsLineSegment: (lineSegment) ->
    if lineSegment.slope == @slope
      if lineSegment.y0 == @y0
        if lineSegment.left.x == @left.x or lineSegment.left.x == @right.x or lineSegment.right.x == @right.x or lineSegment.right.x == @left.x
          return true
        else
          [left, right] = if lineSegment.left.x < @left.x then [lineSegment, @] else [@, lineSegment]
          if left.right.x > right.left.x
            return true
    else
      x = (lineSegment.y0 - @y0) / (@slope - lineSegment.slope)
      if x >= @left.x and x <= @right.x
        return true
    false

  pointOnLine: (point, segment=true) ->
    if point.y == @y(point.x)
      if segment
        [littleY, bigY] = if @a.y < @b.y then [@a.y, @b.y] else [@b.y, @a.y]
        if littleY <= point.y and bigY >= point.y
          return true
    else
      return true
    false



class Thang
  constructor: (@pos, @width=1, @height=1, @depth=1, @shape="box", @rotation=0) ->
    @pos = new Vector(@pos?.x or 0, @pos?.y or 0, @pos?.z or @depth / 2) unless @pos?.isVector

  rectangle: ->
    new Rectangle @pos.x, @pos.y, @width, @height, @rotation

  ellipse: ->
    new Ellipse @pos.x, @pos.y, @width, @height, @rotation

  isGrounded: ->
    @pos.z <= @depth / 2

  isAirborne: ->
    @pos.z > @depth / 2

  contains: (thang) ->
    # Determines whether thang's center is within our bounds.
    if @shape in ["ellipsoid", "disc"]
      @ellipse().containsPoint thang.pos
    else  # box, rectangle
      @rectangle().containsPoint thang.pos

  distance: (thang) ->
    # Determines the distance between the closest edges of @ and thang (0 if touching)
    # TODO: make this aware of the shapes involved
    # TODO: do it at all
    # http://uclue.com/?xq=4737
    # http://stackoverflow.com/questions/401847/circle-rectangle-collision-detection-intersection
    # http://www.gamasutra.com/view/feature/131598/advanced_collision_detection_.php?print=1
    if thang.isVector
      return @pos.distance thang
    if @contains(thang) or @intersects(thang) or thang.contains(@) or thang.intersects(@)
      return 0
    
    [elliptical, rectangular] = [["ellipsoid", "disc"], ["box", "sheet"]]
    if thang.shape in rectangular and @shape in rectangular
      return @.rectangle().distanceToRectangle(thang.rectangle())
    @pos.distance thang.pos

  distanceSquared: (thang) ->
    if thang.isVector
      return @pos.distanceSquared thang
    @pos.distanceSquared thang.pos

  intersects: (thang, t1=null) ->
    [elliptical, rectangular] = [["ellipsoid", "disc"], ["box", "sheet"]]
    t1 ?= @
    t2 = thang
    if t1.contains(t2)
      return true
    if t1.shape in elliptical and t2.shape in elliptical
      thisEllipse = t1.ellipse()
      thatEllipse = t2.ellipse()
      return thisEllipse.intersectsEllipse(thatEllipse)
    if (t1.shape in elliptical and t2.shape in rectangular) or (t1.shape in rectangular and t2.shape in elliptical)
      [ellipse, rectangle] = if t1.shape in elliptical then [t1.ellipse(), t2.rectangle()] else [t2.ellipse(), t1.rectangle()]
      vertices = rectangle.vertices()
      lineSegments = [[vertices[0], vertices[1]], [vertices[1], vertices[2]], [vertices[2], vertices[3]], [vertices[3], vertices[0]]]
      for lineSegment in lineSegments
        [p1, p2] = [lineSegment[0], lineSegment[1]]
        if ellipse.intersectsLineSegment(p1, p2)
          return true
    if t1.shape in rectangular and t2.shape in rectangular
      thisRectangle = t1.rectangle()
      thatRectangle = t2.rectangle()
      if thisRectangle.intersectsRectangle(thatRectangle)
        return true
    false

  toString: ->
    "<#{@shape} - #{@pos.toString()} - #{@width} x #{@height} x #{@depth}"

      
log = (args...) ->
    $('#output').append($('<div></div>').text(args.join(' ')))
    
rect1 = new Thang({x: 0, y: 0}, 8, 2, 0, "box", Math.PI / 100);
log(rect1.rectangle().vertices())
rect2 = new Thang({x: 0, y: 8}, 8, 2, 0, "box", Math.PI / 100);
log(rect2.rectangle().vertices())
log(rect1.intersects(rect2))

log("rectangle-rectangle distance. should be 2.2426: ", 
    new Thang({x: 0, y: 0}, 4, 4, 0, "box", Math.PI / 4).distance(new Thang({x: 4, y: -4}, 2, 2, 0, "box", 0)))
log("rectangle-rectangle distance. one contains the other. should be 0: ", 
    new Thang({x: 0, y: 0}, 3, 3, 0, "box", 0).distance(new Thang({x: 0, y: 0}, 2, 2, 0, "box", Math.PI / 4)))
log("rectangle-rectangle distance. they intersect. should be 0: ", 
    new Thang({x: 0, y: 0}, 3, 3, 0, "box", 0).distance(new Thang({x: 0, y: 0}, 2.5, 2.5, 0, "box", Math.PI / 4)))
log("rectangle-rectangle distance. should be 1: ", 
    new Thang({x: 0, y: 0}, 4, 4, 0, "box", 0).distance(new Thang({x: 4, y: 2}, 2, 2, 0, "box", 0)))



ellipse = new Thang({x: 1, y: 2}, 4, 6, 0, "ellipsoid", Math.PI / 4)
log("ellipse with y major axis, off-origin center, and 45 degree rotation", ellipse)
log("ellipse.contains(1, 2) should be true: ", ellipse.contains(new Thang({x: 1, y: 2}, 0, 0, 0, "ellipsoid")))
log("ellipse.contains(-1, 3) should be true: ", ellipse.contains(new Thang({x: -1, y: 3}, 0, 0, 0, "ellipsoid")))
log("ellipse.contains(0, 4) should be true: ", ellipse.contains(new Thang({x: 0, y: 4}, 0, 0, 0, "ellipsoid")))
log("ellipse.contains(1, 4) should be true: ", ellipse.contains(new Thang({x: 1, y: 4}, 0, 0, 0, "ellipsoid")))
log("ellipse.contains(3, 0) should be true: ", ellipse.contains(new Thang({x: 3, y: 0}, 0, 0, 0, "ellipsoid")))
log("ellipse.contains(1, 0) should be true: ", ellipse.contains(new Thang({x: 1, y: 0}, 0, 0, 0, "ellipsoid")))
log("ellipse.contains(0, 1) should be true: ", ellipse.contains(new Thang({x: 0, y: 1}, 0, 0, 0, "ellipsoid")))
log("ellipse.contains(-1, 2) should be true: ", ellipse.contains(new Thang({x: -1, y: 2}, 0, 0, 0, "ellipsoid")))
log("ellipse.contains(2, 2) should be true: ", ellipse.contains(new Thang({x: 2, y: 2}, 0, 0, 0, "ellipsoid")))
log("ellipse.contains(0, 0) should be false: ", ellipse.contains(new Thang({x: 0, y: 0}, 0, 0, 0, "ellipsoid")))
log("ellipse.contains(0, 5) should be false: ", ellipse.contains(new Thang({x: 0, y: 5}, 0, 0, 0, "ellipsoid")))
log("ellipse.contains(3, 4) should be false: ", ellipse.contains(new Thang({x: 3, y: 4}, 0, 0, 0, "ellipsoid")))
log("ellipse.contains(4, 0) should be false: ", ellipse.contains(new Thang({x: 4, y: 0}, 0, 0, 0, "ellipsoid")))
log("ellipse.contains(2, -1) should be false: ", ellipse.contains(new Thang({x: 2, y: -1}, 0, 0, 0, "ellipsoid")))
log("ellipse.contains(0, -3) should be false: ", ellipse.contains(new Thang({x: 0, y: -3}, 0, 0, 0, "ellipsoid")))
log("ellipse.contains(-2, -2) should be false: ", ellipse.contains(new Thang({x: -2, y: -2}, 0, 0, 0, "ellipsoid")))
log("ellipse.contains(-2, 0) should be false: ", ellipse.contains(new Thang({x: -2, y: 0}, 0, 0, 0, "ellipsoid")))
log("ellipse.contains(-2, 4) should be false: ", ellipse.contains(new Thang({x: -2, y: 4}, 0, 0, 0, "ellipsoid")))
log("ellipse.intersects(rectangle: 0, 0, 2, 2, 0) should be true: ", ellipse.intersects(new Thang({x: 0, y: 0}, 2, 2, 0, "box", 0)))
log("ellipse.intersects(rectangle: 0, -1, 2, 3, 0) should be true: ", ellipse.intersects(new Thang({x: 0, y: -1}, 2, 3, 0, "box", 0)))
log("ellipse.intersects(rectangle: -1, -0.5, 2rt2, 2rt2, 45) should be true: ", ellipse.intersects(new Thang({x: -1, y: -0.5}, 2 * Math.SQRT2, 2 * Math.SQRT2, 0, "box", Math.PI / 4)))
log("ellipse.intersects(rectangle: -1, -0.5, 2rt2, 2rt2, 0) should be true: ", ellipse.intersects(new Thang({x: -1, y: -0.5}, 2 * Math.SQRT2, 2 * Math.SQRT2, 0, "box", 0)))
log("ellipse.intersects(rectangle: -1, -1, 2rt2, 2rt2, 0) should be true: ", ellipse.intersects(new Thang({x: -1, y: -1}, 2 * Math.SQRT2, 2 * Math.SQRT2, 0, "box", 0)))
log("ellipse.intersects(rectangle: -1, -1, 2rt2, 2rt2, 45) should be false: ", ellipse.intersects(new Thang({x: -1, y: -1}, 2 * Math.SQRT2, 2 * Math.SQRT2, 0, "box", Math.PI / 4)))
log("ellipse.intersects(rectangle: -2, -2, 2, 2, 0) should be false: ", ellipse.intersects(new Thang({x: -2, y: -2}, 2, 2, 0, "box", 0)))
log("ellipse.intersects(rectangle: -Math.SQRT2 / 2, -Math.SQRT2 / 2, Math.SQRT2, Math.SQRT2, 0) should be false: ", ellipse.intersects(new Thang({x: -Math.SQRT2 / 2, y: -Math.SQRT2 / 2}, Math.SQRT2, Math.SQRT2, 0, "box", 0)))
log("ellipse.intersects(rectangle: -Math.SQRT2 / 2, -Math.SQRT2 / 2, Math.SQRT2, Math.SQRT2, Math.PI / 4) should be false: ", ellipse.intersects(new Thang({x: -Math.SQRT2 / 2, y: -Math.SQRT2 / 2}, Math.SQRT2, Math.SQRT2, 0, "box", Math.PI / 4)))
log("ellipse.intersects(rectangle: -2, 0, 2, 2, 0) should be false: ", ellipse.intersects(new Thang({x: -2, y: 0}, 2, 2, 0, "box", 0)))
log("ellipse.intersects(rectangle: 0, -2, 2, 2, 0) should be false: ", ellipse.intersects(new Thang({x: 0, y: -2}, 2, 2, 0, "box", 0)))
rectangle = new Thang({x: 1, y: 1}, 2, 2, 0, "box", 0)
log(rectangle)
log("rectangle.intersects(rectangle: 3, 1, 2, 2, 0) should be true: ", rectangle.intersects(new Thang({x: 3, y: 1}, 2, 2, 0, "box", 0)))
log("rectangle.intersects(rectangle: 3, 3, 2, 2, 0) should be true: ", rectangle.intersects(new Thang({x: 3, y: 3}, 2, 2, 0, "box", 0)))
log("rectangle.intersects(rectangle: 1, 1, 2, 2, 0) should be true: ", rectangle.intersects(new Thang({x: 1, y: 1}, 2, 2, 0, "box", 0)))
log("rectangle.intersects(rectangle: 3, 1, 2, 2, Math.PI / 4) should be true: ", rectangle.intersects(new Thang({x: 3, y: 1}, 2, 2, 0, "box", Math.PI / 4)))
log("rectangle.intersects(rectangle: 1, 3, Math.SQRT1_2, Math.SQRT1_2, Math.PI / 4) should be true: ", rectangle.intersects(new Thang({x: 1, y: 3}, Math.SQRT1_2, Math.SQRT1_2, 0, "box", Math.PI / 4)))
log("rectangle.intersects(rectangle: 1, 3, Math.SQRT1_2, Math.SQRT1_2, Math.PI / 4) should be true: ", rectangle.intersects(new Thang({x: 1, y: 1}, Math.SQRT1_2, Math.SQRT1_2, 0, "box", Math.PI / 4)))
log("rectangle.intersects(rectangle: 4, 1, 2, 2, 0) should be false: ", rectangle.intersects(new Thang({x: 4, y: 1}, 2, 2, 0, "box", 0)))
log("rectangle.intersects(rectangle: 3, 4, 2, 2, 0) should be false: ", rectangle.intersects(new Thang({x: 3, y: 4}, 2, 2, 0, "box", 0)))
log("rectangle.intersects(rectangle: 1, 4, 2 * Math.SQRT2, 2 * Math.SQRT2, Math.PI / 4) should be false: ", rectangle.intersects(new Thang({x: 1, y: 4}, Math.SQRT1_2, Math.SQRT1_2, 0, "box", Math.PI / 4)))
              
            
!
999px

Console