Created
April 21, 2020 20:49
-
-
Save erenkeskin/e523a0df74d46b1670469ff1a30daa55 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# for graphics library: https://mcsp.wartburg.edu/zelle/python/graphics.py | |
import math | |
import random | |
import graphics | |
POINT_MIN = 0 | |
POINT_MAX = 250 | |
WINDOW_WIDTH = 300 | |
WINDOW_HEIGHT = 300 | |
# General Formula: (Ax(By - Cy) + Bx(Cy - Ay) + Cx(Ay - By)) / 2 | |
def get_area(v1, v2, v3) : | |
area = v1[0] * (v2[1] - v3[1]) | |
area += v2[0] * (v3[1] - v1[1]) | |
area += v3[0] * (v1[1] - v2[1]) | |
return abs(area) / 2 | |
# General Formula: √((x2−x1)2+(y2−y1)2) | |
def get_perimeter(v1, v2, v3): | |
perimeter = math.sqrt( (v2[0] - v1[0])**2 + (v2[1] - v1[1])**2 ) | |
perimeter += math.sqrt( (v3[0] - v2[0])**2 + (v3[1] - v2[1])**2 ) | |
perimeter += math.sqrt( (v3[0] - v1[0])**2 + (v3[1] - v1[1])**2 ) | |
return perimeter | |
# Generate Points | |
point_A = (random.uniform(POINT_MIN, POINT_MAX), random.uniform(POINT_MIN, POINT_MAX)) | |
point_B = (random.uniform(POINT_MIN, POINT_MAX), random.uniform(POINT_MIN, POINT_MAX)) | |
point_C = (random.uniform(POINT_MIN, POINT_MAX), random.uniform(POINT_MIN, POINT_MAX)) | |
# Calculate Area and Perimeter | |
area = get_area(point_A, point_B, point_C) | |
perimeter = get_perimeter(point_A, point_B, point_C) | |
# Print all points and data | |
print("Point A: ", point_A) | |
print("Point B: ", point_B) | |
print("Point C: ", point_C) | |
print("Area: ", area) | |
print("Perimeter: ", perimeter) | |
# Set Configuration for Triangle | |
win = graphics.GraphWin("Triangle", WINDOW_WIDTH, WINDOW_HEIGHT) | |
win.setBackground("white") | |
# Add the (x, y) point to the vertices | |
triangle_points = [ graphics.Point(point_A[0], point_A[1]), | |
graphics.Point(point_B[0], point_B[1]), | |
graphics.Point(point_C[0], point_C[1]) | |
] | |
# Set Polygon | |
p = graphics.Polygon(triangle_points) | |
p.setFill('red') | |
p.draw(win) # Draw Polygon | |
win.getMouse() # Pause to view result | |
win.close() # Close window when done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment