简体   繁体   English

如何用python绘制直角三角形

[英]How to draw right angled triangle with python

def drawTri(a):
    b = (a*math.tan(45))
    c = (a/math.cos(45))

    t.forward(a)
    t.left(135)
    t.forward(c)
    t.left(135)
    t.forward(b)

在此处输入图片说明

import turtle

def drawTri(a):
    hyp = a * 2**0.5
    s = turtle.Screen()
    t = turtle.Turtle()
    t.forward(a)
    t.left(135)
    t.forward(hyp)
    t.left(135)
    t.forward(a)

The problem here is close to that described in Basic trigonometry isn't working correctly in python 这里的问题与基本三角函数中描述的问题非常接近, 无法在python中正常工作

The turtle module uses degrees for angles, the math module uses radians 乌龟模块使用度数表示角度,数学模块使用弧度数

To calculate the cosine of 45 degrees you can use 要计算45度的余弦,您可以使用

math.cos(math.radians(45))

Who needs angles? 谁需要角度?

def drawTri(a):
    x, y = turtle.position()
    turtle.setx(x + a)
    turtle.sety(y + a)
    turtle.goto(x, y)

i made a relay simple way of making a right angle triangle i also added some other helpful things to know about python turtle that is you didn't already know them they should be helpful(i know you already have an answer but i just think that this is a much simpler way) 我使用了一种简单的方法来制作直角三角形,我还添加了一些有关python turtle的有用信息,那就是您还不知道它们应该有用(我知道您已经有了答案,但我只是认为这是一种更简单的方法)

import turtle
t = turtle
f = t.forward
r = t.right
t.color('blue','yellow')
t.begin_fill()
f(70)
r(135)
f(100)
r(135)
f(70)
r(135)
t.end_fill()

t.penup()
t.setposition(-50,30)
t.pendown()
t.color('blue','yellow')
t.begin_fill()
f(70)
r(135)
f(100)
r(135)
f(70)
r(135)
t.end_fill()

there are two triangles there and its the extra stuff that makes it so bulky 那里有两个三角形,其多余的东西使它如此庞大

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM