ID:1746347   Dec 24 2014, 8:46 pm (Edited on Dec 27 2014, 2:24 pm) 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 textSide 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 = 0datum 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 }} ```
 <-> Dec 24 2014, 8:58 pm 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.
 <-> Dec 24 2014, 9:48 pm In response to Kaiochao You're right! Thanks. I didn't think of that.
 <-> Dec 25 2014, 4:15 am I'll never be able to program like this lol.
 <-> Dec 26 2014, 2:18 pm 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.
 <-> Dec 26 2014, 3:51 pm 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 + cy' = d*x + e*y + f ``` Where: ``` a d 0x y 1 * b e 0 = x' y' 1 c f 1 ```
 <-> Dec 27 2014, 2:31 pm (Edited on Dec 27 2014, 2:36 pm) 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() formatObject ID:# { objectVariable.name = objectVariable.toText() variable name = number or string}// Side object formatSide 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.
 <-> Jan 6 2015, 7:20 am Triangles hard. Triangles master geometry race