ID:1746347
 
To help myself understand triangles and how to program with trigonometry a little better I decided to write a triangle class for DM.

One of the main advantages of using a triangle class is that keeps information hidden and also helps by doing things like calculating the length of the sides for you.

Don't expect this to be used by anyone, just looked at.

Triangle
var
Vertex/vertex1
Vertex/vertex2
Vertex/vertex3

Side/side1
Side/side2
Side/side3

New(Vertex/VERTEX1,Vertex/VERTEX2,Vertex/VERTEX3)
..()
if(args.len != 3) return
vertex1 = VERTEX1
vertex2 = VERTEX2
vertex3 = VERTEX3
side1 = new/Side(vertex1,vertex2)
side2 = new/Side(vertex2,vertex3)
side3 = new/Side(vertex3,vertex1)

proc
perimeter()
return side1.length + side2.length + side3.length

toText()
var/text = "Triangle ID:[ID] {\n"
text += "\tvertex1 = [vertex1.toText()]\n"
text += "\tvertex2 = [vertex2.toText()]\n"
text += "\tvertex3 = [vertex3.toText()]\n\n"
text += "\tside1 = [side1.toText()]\n"
text += "\tside2 = [side2.toText()]\n"
text += "\tside3 = [side3.toText()]\n"
text += "}"
return text

Side
var
Vertex/vertex1
Vertex/vertex2
length = 0

New(VERTEX1, VERTEX2)
..()
if(args.len != 2) return
vertex1 = VERTEX1
vertex2 = VERTEX2
calcLength()

proc
calcLength()
var/sideA = vertex1.x - vertex2.x
var/sideB = vertex1.y - vertex2.y
length = sqrt(sideA ** 2 + sideB ** 2)

toText()
return "Side ID:[ID] { [vertex1.toText()] >> [vertex2.toText()] = [length] }"

Vertex
var
x = 0
y = 0

New(X,Y)
..()
x = X
y = Y

proc
toText()
return "Vertex ID:[ID] { ([x],[y]) }"

var/currentID = 0
datum
var/ID = 0

New()
ID = currentID
currentID ++


Example Output:
Triangle ID:4 {
vertex1 = Vertex ID:1 { (2,2) }
vertex2 = Vertex ID:2 { (4,4) }
vertex3 = Vertex ID:3 { (5,0) }

side1 = Side ID:5 { Vertex ID:1 { (2,2) } >> Vertex ID:2 { (4,4) } = 2.82843 }
side2 = Side ID:6 { Vertex ID:2 { (4,4) } >> Vertex ID:3 { (5,0) } = 4.12311 }
side3 = Side ID:7 { Vertex ID:3 { (5,0) } >> Vertex ID:1 { (2,2) } = 3.60555 }
}
To better understand trigonometry, you should know that using absolute value in calcLength() is unnecessary. Squaring a real number always results in a positive number.
In response to Kaiochao
You're right! Thanks. I didn't think of that.
I'll never be able to program like this lol.
In response to Ganite
You will in time, this isn't even very complicated really. A triangle has three sides and three points. All I'm doing is storing that information in a Triangle object.
That output is pretty confusing! Seems like it wants to be JSON really badly but just doesn't seem to know how to be JSON.

For added learning fun, try to figure out how to directly translate the three vertexes when introduced to 3x2 (or 3x3) matrix. The BYOND docs already kinda give you the formula you need:

x' = a*x + b*y + c
y' = d*x + e*y + f


Where:
          a d 0
x y 1 * b e 0 = x' y' 1
c f 1
Doohl wrote:
That output is pretty confusing! Seems like it wants to be JSON really badly but just doesn't seem to know how to be JSON.

I didn't know about JSON actually, had to google it. This is the current format I use for output in my projects but I'll look into changing it. I probably should have included a definition of the format as well actually.

// toText() format

Object ID:# {
objectVariable.name = objectVariable.toText()
variable name = number or string
}

// Side object format

Side ID:# { Vertex1.toText() >> Vertex2.toText() = length of side }


GatewayRa wrote:
Well, I'm so happy for you that you know about triangles now.

Glad you're happy.
Triangles hard. Triangles master geometry race