我正在尝试为我的班级创建一个程序,该程序在一个圆内创建一个三角形,所有顶点都接触该圆的周长。如同 this:
这是我的代码:
// Lab06Cst.java
// The Expo Graphics Program II
// This is the student, starting version, of Lab 06C.
import java.awt.*;
import java.applet.*;
public class TimothyG_Lab06Cst extends Applet
{
public void paint(Graphics g)
{
// Substitute your own name here.
Expo.drawHeading(g,"Timothy Grant","6C");
//EXPO
/* E */
Expo.fillRectangle(g,50,60,60,110);
Expo.fillRectangle(g,50,60,80,70);
Expo.fillRectangle(g,50,80,70,90);
Expo.fillRectangle(g,50,100,80,110);
/* X */
Expo.fillRectangle(g,90,60,100,80);
Expo.fillRectangle(g,90,90,100,110);
Expo.fillRectangle(g,100,80,110,90);
Expo.fillRectangle(g,110,60,120,80);
Expo.fillRectangle(g,110,90,120,110);
/* P */
Expo.fillRectangle(g,130,60,140,110);
Expo.fillRectangle(g,140,60,160,70);
Expo.fillRectangle(g,150,70,160,90);
Expo.fillRectangle(g,140,80,150,90);
/* O */
Expo.fillCircle(g,195,85,25);
Expo.setColor(g,255,255,255);
Expo.fillCircle(g,195,85,15);
Expo.setColor(g,0);
//Pentagon
Expo.drawRegularPolygon(g,50,160,30,5);
//Symbol
double r = 40.0;
double angle1 = Math.random()* (2 * Math.PI);
double angle2 = Math.random()* (2 * Math.PI);
double angle3 = Math.random()* (2 * Math.PI);
double x_1 = r * Math.cos(angle1);
double y_1 = r * Math.sin(angle1);
double x_2 = r * Math.cos(angle2);
double y_2 = r * Math.sin(angle2);
double x_3 = r * Math.cos(angle3);
double y_3 = r * Math.sin(angle3);
double a = Math.sqrt(Math.pow(x_2 - x_1, 2) + Math.pow(y_2 - y_1, 2));
double b = Math.sqrt(Math.pow(x_3 - x_2, 2) + Math.pow(y_3 - y_2, 2));
double c = Math.sqrt(Math.pow(x_1 - x_3, 2) + Math.pow(y_1 - y_3, 2));
Expo.drawCircle(g,125,230,40); //The circle the triangle goes in
Expo.drawLine(g, (int) x_1, (int) y_1, (int) x_2, (int) y_2);
Expo.drawLine(g, (int) x_2, (int) y_2, (int) x_3, (int) y_3); //Should be drawing the triangle
Expo.drawLine(g, (int) x_3, (int) y_3, (int) x_1, (int) y_1);
}
}
“符号”的代码应该在圆中创建一个三角形,但是它只是在0,0附近创建一个三角形,并且当我移动applet窗口时它会散开。博览会文档为here
答案 0 :(得分:0)
这是您的问题:您需要向圆心添加一个偏移量。
double center_x = 125 + 40/2; //x start + 1/2 * radius
double center_y = 230 + 40/2; //y start + 1/2 * radius
double x_1 = center_x + r * Math.cos(angle1);
double y_1 = center_y + r * Math.sin(angle1);
double x_2 = center_x + r * Math.cos(angle2);
double y_2 = center_y + r * Math.sin(angle2);
double x_3 = center_x + r * Math.cos(angle3);
double y_3 = center_y + r * Math.sin(angle3);