【问题标题】:Create Triangle Inside Circle在圆内创建三角形
【发布时间】:2020-02-11 20:31:28
【问题描述】:

我正在尝试为我的班级创建一个程序,该程序在圆内创建一个三角形,所有顶点都接触圆的周边。如同 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 附近创建三角形,并在我移动小程序窗口时显示出来。世博文档是here

【问题讨论】:

标签: java applet expo trigonometry


【解决方案1】:

这是您的问题:您需要向圆心添加偏移量。

  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);

【讨论】:

  • 在这之后我应该找到三角形的质心以及质心和周长之间的距离,以便在三角形内放置另一个圆吗?我可以这样做,但我不知道还有什么。
  • @TimothyGrant 我无法回答您的问题,但请记得点击我的答案上的复选标记以标记此问题已回答。
  • 对不起,我忘了。修好了!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-23
  • 1970-01-01
  • 2015-08-12
  • 1970-01-01
  • 2015-06-10
相关资源
最近更新 更多