简体   繁体   English

创建以质心为中心的三角形的图像

[英]Creating an image of a triangle centered at the centroid

Excuse me as I am a little new to programming. 对不起,因为我是编程新手。 Basically, I am assigned to "analyze" and produce an image of a triangle where the user specifies the lengths of two sides and the size of the angle between them, the program runs and finds the length of the third side as well as the two other angles (using Law of Cosines). 基本上,我被分配给“分析”并生成一个三角形的图像,其中用户指定了两侧的长度以及它们之间的角度大小,程序运行并找到了第三侧以及两个侧面的长度其他角度(使用余弦定律)。 Then I must have text outputs for all sides lengths, all angle measures (optional), and also print the area in the form of my turtle printing out "Here is your triangle \\n It has an area of x square pixels" in the image as well. 然后,我必须具有所有边长,所有角度尺寸(可选)的文本输出,并以乌龟的形式打印该区域,并在图像中打印出“这是您的三角形\\ n,它的区域为x正方形像素”也一样 Also, the triangle must have its centroid at (0,0). 同样,三角形的质心必须位于(0,0)。 This is what I have so far: 这是我到目前为止的内容:

import math
from turtle import*



print("This program will draw a custom triangle.")
firstside=float(input("Enter length of first side (between 10 and 400 pixels): "))
secondside=float(input("Enter length of second side (between 10 and 400 pixels): "))
includedangle=float(input("Enter the measure of the included angle in degrees (between 1 and 179): "))

print("Side lengths and angle measures:\n")





solvedthirdside=float(math.sqrt(firstside**2+secondside**2-(2*firstside*secondside*math.cos(includedangle))))

cage=Screen(); #Create screen object named cage
cage.setup(500,500) #set screen size to 500x500 pixels

Also, I am using the turtle graphics for this too. 另外,我也为此使用了乌龟图形。

I am really struggling with this. 我真的为此感到挣扎。 Some help would be greatly appreciated! 一些帮助将不胜感激! Please help! 请帮忙! Thanks! 谢谢!

This is not finished, but should be a lot easier to work on now. 这还没有完成,但是现在应该容易得多。

I have thoroughly factored it into functions and commented heavily; 我已将其完全分解为功能,并发表了大量评论; it should be pretty easy to understand. 它应该很容易理解。 Hope that helps! 希望有帮助!

import math
import turtle

def get_float(prompt):
    """
    Prompt for input and return it as a floating-point number
    """
    while True:
        try:
            return float(input(prompt))
        except ValueError:
            pass

def third_side(a, b, C):
    """
    Given a triangle defined by two sides and
    the angle between them (in degrees),
    return the length of the third side
    """
    return math.sqrt(a**2 + b**2 - 2. * a * b * math.cos(math.radians(C)))

def get_sides():
    """
    Prompt for a triangle as two sides and an angle,
    calculate the length of the third side,
    and return the three side lengths
    """
    side1 = get_float("Length of first side  (pixels, in 10..400)? ")
    side2 = get_float("Length of second side (pixels, in 10..400)? ")
    angle = get_float("Included angle (degrees, in 1..179)? ")
    return side1, side2, third_side(side1, side2, angle)

def get_angle(opp, left, right):
    """
    Given three sides of a triangle,
    return the angle opposite the first side (in degrees)
    """
    cos_opp = (left**2 + right**2 - opp**2) / (2. * left * right)
    return math.degrees(math.acos(cos_opp))

def get_angles(a, b, c):
    """
    Given three sides of a triangle,
    return the three angles (in degrees)
    """
    return get_angle(a, b, c), get_angle(b, c, a), get_angle(c, a, b)

def main():
    print("This program will draw a custom triangle.\n")
    side1, side2, side3    = get_sides()
    angle1, angle2, angle3 = get_angles(side1, side2, side3)

    print(
        "\n"
        "Sides and angles:\n"
        "a = {side1:>5.1f}    A = {angle1:>5.1f}\n"
        "b = {side2:>5.1f}    B = {angle2:>5.1f}\n"
        "c = {side3:>5.1f}    C = {angle3:>5.1f}\n"
        .format(
            side1=side1,   side2=side2,   side3=side3,
            angle1=angle1, angle2=angle2, angle3=angle3
        )
    )

    # Your code continues here!

if __name__=="__main_":
    main()

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

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