Manhattan / Taxicab Distance Calculator
Edit in new window
bubble
play_circle
Run
stop_circle
Stop
cancel
Clear
text_increase
text_decrease
light_mode
routine
dark_mode
restart_alt
Reset
download_for_offline
Download
help
Help
main.py
draw.py
⨯
+
#Manhattan/taxicab Distance Calculator - www.101computing.net/manhattan-distance-calculator/ import turtle, draw def straightDistance(xA,yA,xB,yB): distance = ((xA-xB)**2 + (yA-yB)**2)**0.5 return distance def taxicabDistance(xA,yA,xB,yB): distance = abs(xA-xB) + abs(yA-yB) return distance #Generate two sets of cooridnates and plot them on the canvas xA,yA,xB,yB = draw.drawAB() #Output straight distance print("Straight distance:") print(straightDistance(xA,yA,xB,yB)) #Output taxicab distance print("Taxicab distance:") print(taxicabDistance(xA,yA,xB,yB))
import turtle from random import randint def drawAB(): myPen = turtle.Turtle() myPen.hideturtle() myPen.tracer(0) myPen.speed(0) window = turtle.Screen() window.bgcolor("#F0F0F0") xA = randint(-180,180) yA = randint(-180,180) xB = randint(-180,180) yB = randint(-180,180) myPen.color("#ff5744") myPen.penup() myPen.goto(xA-5,yA) myPen.pendown() myPen.goto(xA+5,yA) myPen.penup() myPen.goto(xA,yA-5) myPen.pendown() myPen.goto(xA,yA+5) myPen.penup() myPen.color("#41befc") myPen.penup() myPen.goto(xB-5,yB) myPen.pendown() myPen.goto(xB+5,yB) myPen.penup() myPen.goto(xB,yB-5) myPen.pendown() myPen.goto(xB,yB+5) myPen.penup() myPen.getscreen().update() return xA,yA,xB,yB
task_alt