Создать треугольник внутри круга - PullRequest
0 голосов
/ 11 февраля 2020

Я пытаюсь создать программу для своего класса, которая создает треугольник внутри круга со всеми вершинами, касающимися периметра круга. Аналогично 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 и spaszes, когда я перемещаю окно апплета Документ Экспо здесь

1 Ответ

0 голосов
/ 11 февраля 2020

Вот ваша проблема: вам нужно добавить смещение к центру круга.

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