我想做的是創建一個程式,該程式需要一個邊長和兩個角,并使用 AWT 庫生成一個三角形。
我很難找出使用三角比率來產生多邊形的 X 和 Y 點的邏輯。
例如,給定兩個角和一條邊,我試圖計算出下一個 X 和 Y 點,以便將線條畫到那里。
目前,我只是定義了一些任意的點來繪制一個三角形。
圖形下面的代碼塊是用于計算三角形所有邊的數學運算
有人能提供幫助嗎?這是我的代碼。
這是我的代碼。它寫得很糟糕。這主要是我在做實驗和學習更多的java知識。
import javax.swing.*;
import java.awt.*;
import java.math.*;
import java.awt.Component。
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Polygon;
public class program{
public static void main (String[] args){
JFrame frame = new JFrame()。
CustomPaintComponent c = new CustomPaintComponent();
frame.add(c);
frame.setSize(1000,1000) 。
frame.setLocationRelativeTo(null)。
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE)。
frame.setTitle("Triangle")。
frame.setVisible(true)。
}
class CustomPaintComponent extendsComponent {
public void paint(Graphics g){
Graphics2D G2D =(Graphics2D)g。
int[] xpoints = { 500, 750 , 1000 , 500};
int[] ypoints = { 500 , 250 , 500 , 500};
G2D.drawPolygon(xpoints,ypoints,4)。
}
public三角形calc(){
double s1,a1,a2。
s1 = Integer.parseInt(JOptionPane.showInputDialog(" Please enter side 1"))。
a1 = Integer.parseInt(JOptionPane.showInputDialog("請輸入角度1"/span>))。
a2 = Integer.parseInt(JOptionPane.showInputDialog("請輸入角度2"/span>))。
double s2 = ((s1/Math.sin(Math.toRadians(a1)) ) *Math.sin(Math.toRadians(a2)))。)
System.out.println(s1/Math.sin(a1))。
System.out.println(Math.sin(a2))。
double a3 = 180 - (a1 a2)。
double s3 = Math.pow(s2, 2) Math. pow(s1, 2) - (2 * s2 * s1 * Math.cos(Math.toRadians(a3))。
s3 = Math.sqrt(s3);
Math.toDegrees(a1);
Math.toDegrees(a2);
Math.toDegrees(a3)。
System.out.println("邊1是"/span> s1 "邊2是"/span> s2 " 而第三個角是。" a3 ",第三邊長是:" a3 ",第三邊長是。" s3)。
三角形 三角形 = new 三角形(s1, s2,s3,a1,a2,a3)。)
return Triangle;
}
class Triangle{
double s1,s2,s3,a1,a2,a3;
Triangle(double s1, double s2, double s3。double a1, double a2, double a3){
this.s1 = s1。
this.s2 = s2;
this.s3 = s3。
this.a1 = a1;
this.a2 = a2;
this.a3 = a3;
}
uj5u.com熱心網友回復:
一個三角形必須滿足的三個規則:
各角相加始終為180°。
A B C = 180度
正弦定律(The Sine Rule) a/sin(A) = b/sin(B) = c/sin(C)
- 注意:角A與邊a相對,B與邊b相對,C與邊c相對。
余弦定律(余弦規則) c^2 = a^2 b^2 - 2ab*cos(C)
所以,假設你有一個三角形XYZ.
。與邊x相對的角=X(=YXZ),與邊y相對的角=Y(=XYZ),與邊z相對的角=Z(=XZY)。
用戶的輸入是兩個角度和它們之間的邊。
設輸入為。
Angle X = 76度
Angle Y = 34度
Side z = 9 cm
首先,通過使用第一個屬性找到第三個角Z。
Angle Z = 180 - X - Y
= 180 - 76 - 34 - X - Y
= 70度
現在,利用正弦定律,找出邊x和y。
x/sin(X) = z/sin(Z)
=> x/sin(76°) = 9/sin(70°)
=> x = (9/sin(70°)) × sin(76°)。
=> x = 9.29 cm
y/sin(Y) = z/sin(Z)
=> y/sin(34°) = 9/sin(70°)
=> y = (9/sin(70°)) × sin(34°)。
=> y = 5.36 cm
要撰寫代碼:
static void printSidesOfTriangle(double side3, double angle1, double angle2) {
//三角形的所有角度之和為180度。
double angle3 = 180 - angle1 - angle2。
//將角度從度數轉換為弧度
angle1 = Math.toRadians(angle1);
angle2 = Math.toRadians(angle2);
angle3 = Math.toRadians(angle3);
double ratio = side3/(Math.sin(angle3)) 。
double side1 = ratio*(Math.sin(angle1))。
double side2 = ratio*(Math.sin(angle2))。
System.out.println("traingle的邊是。"
String.valueOf(side1) ", "/span>
String.valueOf(side2) ", and "
String.valueOf(side3) "。")。)
}
而要得到三角形每個點的x和y坐標,你需要掌握一些更多的資訊,因為在一個二維平面上可能有多個3邊的三角形。
另外,你可以假設其中一個坐標為(0,0),并找到兩邊的斜率。 這樣一來,你就可以得到剩余點的x和y坐標,因為你有這兩條線段的長度。
讓的假設。
角度X=76度
角度Y=34度
角度Z=70度
邊X=9.29厘米
邊y=5.36厘米
側面Z = 9.00厘米
假設三個頂點為(x1,y1)、(x2,y2)和(x3,y3)。
檢查一下這個鏈接
通過這種方式,你可以得到剩余頂點的x和y坐標。
要撰寫代碼:
//printVerticesOfTriangle(5.35,9.29,9.0,34,76,70);
//printVerticesOfTriangle(3,4,5,37,53,90);
static void printVerticesOfTriangle(double side1, double side2, double side,
double angle1, double angle2, double angle3)。
{
// side1與angle1相對。
//side2與angle2相對。
//side3與angle3相對。
//頂點是(x1,y1),(x2,y2)和(x3,y3)。
double x1 = 0。
double y1 = 0;
double x2 = 0;
double y2 = 0;
double x3 = 0;
double y3 = 0;
//如果一個角度是90度,保持該頂點為原點。
if(angle1==90){
x1 = 0;
y1 = 0;
x2 = side2;
y2 = 0;
x3 = 0;
y3 = side3。
}
else if(angle2==90){
x2 = 0;
y2 = 0;
x1 = side1;
y1 = 0;
x3 = 0;
y3 = side3。
}
else if(angle3==90){
x3 = 0;
y3 = 0;
x1 = side1;
y1 = 0;
x2 = 0;
y2 = side2。
}
//如果它不是一個直角三角形。
else{
///將角度從度轉換為弧度 else{ Mr.
angle1 = Math.toRadians(angle1);
angle2 = Math.toRadians(angle2);
//對于斜率,考慮與x軸正向的角度。
angle3 = Math.toRadians(180-angle3)。
x1 = 0;
y1 = 0;
x3 = side2;
y3 = 0;
//頂點(x2,y2)必須被計算出來。
x2 = x3*Math.tan(angle3)/(Math.tan(angle1)-Math.tan(angle2))。
y2 = x2*Math.tan(angle1)。
}
System.out.println("這個三角形的頂點是。"
"(" String.format("%.2f",x1) "," String. format("%.2f",y1) "), " String.
"(" String.format("%.2f",x2) "," String. format("%.2f",y2) "), " String.
"(" String.format("%.2f",x3) "," String. format("%.2f",y3) ")")。
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/310262.html
標籤:
