如何绘制一个随机三角形及其中位数?

时间:2016-10-27 01:54:36

标签: python turtle-graphics

我想画一个这样的三角形:

enter image description here

我尝试过不同的解决方法,但我还没有正确完成。如何在三角形中添加中值线?有人可以帮忙解释一下吗?

from turtle import *
import random

def allTriMedian (w=300):
    speed (0)
    vertices = []
    point = turtle.Point(x,y)

    for i in range (3):
        x = random.randint(0,300)
        y = random.randint(0,300)
        vertices.append(trutle.Point(x,y))
        point = turtle.Point(x,y)
        triangle = turtle.Polygon(vertices)

    a = triangle.side()
    b = triangle.side() 
    c = triangle.side()  
    m1 = tirangle.median
    m2 = triangle.median
    m3 = triangle.median

我试着直接把等式放到

def Median (a, b, c):
    m1 = sqrt((((2b^2)+(2c^2)-(a^2))))
    m2 = sqrt((((2a^2)+(2c^2)-(b^2))))
    m3 = sqrt((((2a^2)+(2b^2)-(c^2))))
    triangle.setFill("yellow")
    triangle.draw(allTriMedian)

或者我想找到一个中点并画一条线段来连接顶点和中点。

def getMid(p1,p2):
      return ( (p1[0]+p2[0]) / 2, (p1[1] + p2[1]))
      mid1 = Line((point(p1[0]+p2[0]) / 2),point(x))
      mid2 = Line((point(p2[1]+p3[1]) / 2),point(y))

3 个答案:

答案 0 :(得分:2)

我讨厌做数学。让我们看看我们是否可以通过向问题投掷海龟来解决这个问题。很多海龟。

我们将随机生成三角形的顶点。依次采取成对的顶点,我们将在每个朝向另一个的方向上开始一只乌龟。当海龟碰撞时(在中点),我们将消灭一只乌龟并将另一只乌龟发送到不在该对中的顶点。一旦我们完成这三次(有六只乌龟),我们应该有问题。好吧,主要是(没有填写我的解决方案):

from turtle import Turtle, Screen
from random import seed, randint

WIDTH, HEIGHT = 640, 480

def meet_in_the_middle(turtle_1, turtle_2):

    position_2 = turtle_2.position()

    while True:
        turtle_1.setheading(turtle_1.towards(turtle_2))
        turtle_1.forward(1)
        position_1 = turtle_1.position()
        if int(position_1[0]) == int(position_2[0]) and int(position_1[1]) == int(position_2[1]):
            break

        turtle_2.setheading(turtle_2.towards(turtle_1))
        turtle_2.forward(1)
        position_2 = turtle_2.position()
        if int(position_2[0]) == int(position_1[0]) and int(position_2[1]) == int(position_1[1]):
            break

seed()

screen = Screen()
screen.setup(WIDTH * 1.25, HEIGHT * 1.25)

vertices = []

for _ in range(3):
    x = randint(-WIDTH//2, WIDTH//2)
    y = randint(-HEIGHT//2, HEIGHT//2)
    vertices.append((x, y))

A, B, C = vertices

turtle_AtoB = Turtle(shape='turtle')
turtle_AtoB.penup()
turtle_AtoB.goto(A)
turtle_AtoB.pendown()

turtle_BtoA = Turtle(shape='turtle')
turtle_BtoA.penup()
turtle_BtoA.goto(B)
turtle_BtoA.pendown()

meet_in_the_middle(turtle_AtoB, turtle_BtoA)

turtle_BtoA.hideturtle()
turtle_AtoB.setheading(turtle_AtoB.towards(C))
turtle_AtoB.goto(C)
turtle_AtoB.hideturtle()


turtle_BtoC = Turtle(shape='turtle')
turtle_BtoC.penup()
turtle_BtoC.goto(B)
turtle_BtoC.pendown()

turtle_CtoB = Turtle(shape='turtle')
turtle_CtoB.penup()
turtle_CtoB.goto(C)
turtle_CtoB.pendown()

meet_in_the_middle(turtle_BtoC, turtle_CtoB)

turtle_CtoB.hideturtle()
turtle_BtoC.setheading(turtle_BtoC.towards(A))
turtle_BtoC.goto(A)
turtle_BtoC.hideturtle()


turtle_CtoA = Turtle(shape='turtle')
turtle_CtoA.penup()
turtle_CtoA.goto(C)
turtle_CtoA.pendown()

turtle_AtoC = Turtle(shape='turtle')
turtle_AtoC.penup()
turtle_AtoC.goto(A)
turtle_AtoC.pendown()

meet_in_the_middle(turtle_CtoA, turtle_AtoC)

turtle_AtoC.hideturtle()
turtle_CtoA.setheading(turtle_CtoA.towards(B))
turtle_CtoA.goto(B)
turtle_CtoA.hideturtle()

screen.exitonclick()

海龟在工作:

enter image description here

完成图纸:

enter image description here

答案 1 :(得分:0)

感谢cdlane,我接受了他的代码,并将一些功能放入函数中,使其更清晰(至少对我而言)

# -*- coding: cp1252 -*-
import turtle
from turtle import Turtle, Screen
from random import seed, randint

WIDTH, HEIGHT = 640, 480
def create_screen(width, height):
    screen = Screen()
    screen.setup(width * 1.25, height * 1.25)
    return screen

def create_points(count,width = WIDTH, height = HEIGHT):
    vertices = []

    for _ in range(count):
        x = randint(-width//2, width//2)
        y = randint(-height//2, height//2)
        vertices.append((x, y))
    return vertices

def create_turtle_at_position(position):
    turtle = Turtle(shape='turtle')
    turtle.hideturtle()
    turtle.penup()
    turtle.goto(position)
    turtle.showturtle()
    turtle.pendown()
    return turtle

def meet_in_the_middle(turtle_1, turtle_2):

    position_2 = turtle_2.position()

    while True:
        turtle_1.setheading(turtle_1.towards(turtle_2))
        turtle_1.forward(1)
        position_1 = turtle_1.position()
        if int(position_1[0]) == int(position_2[0]) and int(position_1[1]) == int(position_2[1]):
            break

        turtle_2.setheading(turtle_2.towards(turtle_1))
        turtle_2.forward(1)
        position_2 = turtle_2.position()
        if int(position_2[0]) == int(position_1[0]) and int(position_2[1]) == int(position_1[1]):
            break

    turtle_1.hideturtle()
    turtle_2.hideturtle()

    return create_turtle_at_position(position_2)

def draw_median(P1st, P2nd, POpposite):
    turtle_AtoB = create_turtle_at_position(P1st)
    turtle_BtoA = create_turtle_at_position(P2nd)
    turtle_AandBmiddle = meet_in_the_middle(turtle_AtoB, turtle_BtoA)
    turtle_AandBmiddle.setheading(turtle_AandBmiddle.towards(POpposite))
    turtle_AandBmiddle.goto(POpposite)
    return turtle_AandBmiddle

seed()

sc = create_screen(WIDTH, HEIGHT)
for _ in range(5):
    sc = create_screen(WIDTH, HEIGHT)
    A, B, C = create_points(3)
    draw_median(A,B,C)
    draw_median(B,C,A)
    draw_median(C,A,B)

sc.exitonclick()

答案 2 :(得分:0)

数学它是通过向量计算这个的最简单方法。让我说你有一个三角形ABC,想要画一条从A到BC中间的线,所以你的矢量从A开始,结束于A + AB + 1/2 BC或A + AC + 1/2 CB(矢量)

vue-loader

导致相反的点

的坐标
(ax) + (bx - ax) + 0.5 (cx - bx)
(ay)   (by - ay)       (cy - by)