Skip to content

Instantly share code, notes, and snippets.

@PedroBern
Created March 24, 2019 14:12
Show Gist options
  • Save PedroBern/1a562ab3814e1339f3323a77c93e4afc to your computer and use it in GitHub Desktop.
Save PedroBern/1a562ab3814e1339f3323a77c93e4afc to your computer and use it in GitHub Desktop.
Cálculo da área de polígono desenhado por azimutes e distancia
import turtle as tu
import numpy as np
screen = tu.Screen()
points = [[10,240],[269,330],[170,410],[110,230],[0,260]]
t = tu.Turtle()
t.speed('fastest')
c = tu.Turtle()
c.speed('fastest')
w = tu.Turtle()
w.speed('fastest')
coordsX, coordsY = [], []
def savecoord():
coordsX.append(t.xcor())
coordsY.append(t.ycor())
def text(i, x, y):
w.penup()
w.goto(x + 40, y - 25)
style = ('Courier', 10, 'italic')
w.write(str(points[i][0]) + ' graus', font=style, align='center')
def angle(i):
next = i + 1
X = coordsX[i]
Y = coordsY[i]
if i == len(points) - 1:
X = 0
Y = 0
next = 0
c.penup()
c.goto(X, Y)
c.pendown()
c.setheading(90)
c.pencolor("red")
c.forward(50)
text(next, c.xcor(), c.ycor())
c.setheading(0)
c.circle(-50, points[next][0])
def go(i):
t.right(points[i][0])
t.forward(points[i][1])
savecoord()
angle(i)
for i in range(0, len(points)):
if i is 0:
t.left(90)
else:
t.left(points[i-1][0])
go(i)
if (t.xcor(), t.ycor()) is not (0,0):
t.goto(0,0)
savecoord()
# https://stackoverflow.com/questions/24467972/...
# ...calculate-area-of-polygon-given-x-y-coordinates
def polygon_area(x,y):
x_, y_ = x - np.mean(x), y - np.mean(y)
correction = x_[-1] * y_[0] - y_[-1]* x_[0]
main_area = np.dot(x_[:-1], y_[1:]) - np.dot(y_[:-1], x_[1:])
return 0.5*np.abs(main_area + correction)
print('Area =',polygon_area(coordsX, coordsY))
def perimeter(points):
perimeter = 0
for p in points:
perimeter += p[1]
return perimeter
print('Perimetro =', perimeter(points))
t.hideturtle()
c.hideturtle()
w.hideturtle()
screen.exitonclick()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment