Enter any Triangle as three points on the grid.
The program will plot it (if it fits) and give the three edge lengths in the console.
Close the console to view the triangle.
To fit the grid, I discovered 25 pixels to the side of a grid cell did the trick.
For a more complete Triangle class that doesn't draw anything see:
https://github.com/4dsolutions/Python5/blob/master/triangles.py
from math import sqrt as rt2
stage.set_background("grid2")
sprite = turtle.Turtle()
sprite.hideturtle()
sprite.width(5)
sprite.penup()
def draw_me(A, B, C):
sprite.clear()
sprite.goto(A[0], A[1])
sprite.pendown()
sprite.shape("turtle")
sprite.color("blue")
sprite.goto(B[0], B[1])
sprite.color("red")
sprite.goto(C[0], C[1])
sprite.color("green")
sprite.goto(A[0], A[1])
sprite.penup()
def ask_user():
print "Separate numbers by space"
a,b = input("Point A? ").split()
Ax,Ay = int(a)*25, int(b)*25
a,b = input("Point B? ").split()
Bx,By = int(a)*25, int(b)*25
a,b = input("Point C? ").split()
Cx,Cy = int(a)*25, int(b)*25
draw_me((Ax,Ay),(Bx,By),(Cx,Cy))
t = Triangle((Ax,Ay),(Bx,By),(Cx,Cy))
print("Sides\n a: {}\n b: {}\n c: {}".format(t.a(), t.b(), t.c()))
print("Area:{}".format(t.area()))
class Triangle:
in_radians = True
def __init__(self, Axy, Bxy, Cxy):
self._Axy = Axy
self._Bxy = Bxy
self._Cxy = Cxy
def a(self):
Bx, By = self._Bxy
Cx, Cy = self._Cxy
return (1./25) * rt2((Bx - Cx)**2 + (By - Cy)**2)
def b(self):
Ax, Ay = self._Axy
Cx, Cy = self._Cxy
return (1./25) * rt2((Ax - Cx)**2 + (Ay - Cy)**2)
def c(self):
Ax, Ay = self._Axy
Bx, By = self._Bxy
return (1./25) * rt2((Ax - Bx)**2 + (Ay - By)**2)
def area(self):
"""
Heron's formula
"""
s = (self.a() + self.b() + self.c())/2.0
return rt2(s*(s-self.a())*(s-self.b())*(s-self.c()))
ask_user()
-
Run Code
-
-
Stop Running Code
-
Show Chart
-
Show Console
-
Codesters How To (opens in a new tab)