2016-10-27 16 views
1

ランダムな三角形とその中央値を描画する方法は?私はこのような三角形描きたい

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)) 
+0

の反対ポイントの座標にあなたのコードが有効インデントを持っていない結果

(ax) + (bx - ax) + 0.5 (cx - bx) (ay) (by - ay) (cy - by) 

、マークダウンエディタで '{}'形式のボタンを使用してください。また、StackOverflowは一般的なプログラミングの質問の場です。この特定のコードについては将来の読者には役に立たないと説明していますので、この質問は話題にはなりません。 –

+1

トピックになっていても、あなたの解決策をあなたの望むやり方で説明しているかどうかは分かりません。プログラミングの巨大な部分は、問題解決のためのものです。たとえ問題を解決する方法のプロセスを記述するアルゴリズムを少なくとも理解できるようにするためにコードを書く方法がわからなくても、一度それをコードに入れることができます。 –

答えて

2

私は数学をやっていません。問題にカメを投げることでこれを解決できるかどうかを見てみましょう。たくさんのカメ。

三角形の頂点をランダムに生成します。交互に頂点ペアを取ると、それぞれの見出しにもう1つのタートルが表示されます。亀が衝突すると(中点で)、1頭のカメを排除し、もう1頭をペアにない頂点に向けて送ります。私たちがこれを3回(6匹のカメで)やったら、問題の絵を持っていなければなりません。まあ、ほとんどが(私の解決策には塗りつぶし):職場で

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

完成図面:cdlaneへ

enter image description here

+0

私は数学をするのが好きですが、タートルを使ってこれを行うあなたの解決策は面白いです。 – am2

0

おかげで、私は彼を取っていませんコードを作成し、いくつかの機能を関数に入れて少し明確にする(少なくとも私のために)

# -*- 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() 
0

これはベクトルで計算するのが最も簡単な方法です。三角形のABCがあり、AからBCの真ん中に線を引いて、ベクトルがAから始まり、A + AB + 1/2BCまたはA + AC + 1/2CB(ベクトル)で終わるようにしたいとしましょう。

x = 0.5(cx + bx) 
y = 0.5(cy + by) 
関連する問題