我正在尝试编写一个使用户能够在窗口上单击3个点的代码,然后该代码将自动生成带有所述点的三角形。该代码还用于绘制所生成三角形的内切圆和外切圆。到目前为止,我已经能够使它在适当居中的圆上起作用,并且能够创建外接圆,但是鉴于我无法弄清楚如何计算外接圆这一事实,它无法正确居中三角形的
如果您有任何关于在我的情况下如何计算外心的技巧,我将非常感谢。非常感谢Advanced
import java.awt.*;
import static java.awt.Color.BLACK;
import static java.awt.Color.BLUE;
import static java.awt.Color.RED;
import java.awt.geom.*;
import static java.lang.Math.sqrt;
import javax.swing.JComponent;
public class graficadorTriangulo extends JComponent
{
private double [] coordinatesX;
private double [] coordinatesY;
public graficadorTriangulo (double [] valoresX, double [] valoresY )
{
coordenadasX = valoresX;
coordenadasY = valoresY;
}
public void paintComponent (Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
Point2D.Double click1Pun = new Point2D.Double(coordenadasX[0], coordenadasY[0]);
Point2D.Double click2Pun = new Point2D.Double(coordenadasX[1], coordenadasY[1]);
Point2D.Double click3Pun= new Point2D.Double(coordenadasX[2], coordenadasY[2]);
Point2D.Double click4Pun = new Point2D.Double(coordenadasX[0]/2, coordenadasY[0]/2);
Point2D.Double click1Mitad = new Point2D.Double(coordenadasX[0]/2, coordenadasY[0]/2);
Point2D.Double click2Mitad = new Point2D.Double(coordenadasX[1]/2, coordenadasY[1]/2);
Point2D.Double click3Mitad = new Point2D.Double(coordenadasX[2]/2, coordenadasY[2]/2);
Line2D.Double ladoaLin = new Line2D.Double(click1Pun, click2Pun);
Line2D.Double ladobLin = new Line2D.Double(click1Pun, click3Pun);
Line2D.Double ladocLin = new Line2D.Double(click2Pun, click3Pun);
double magnitudA = Math.sqrt( ( (coordenadasX[1] - coordenadasX[0] ) * ( coordenadasX[1]- coordenadasX[0] ) ) + ( ( coordenadasY[1] - coordenadasY[0] ) * ( coordenadasY[1] - coordenadasY[0] ) ) );
double magnitudB = Math.sqrt( ( (coordenadasX[2] - coordenadasX[1] ) * ( coordenadasX[2]- coordenadasX[1] ) ) + ( ( coordenadasY[2] - coordenadasY[1] ) * ( coordenadasY[2] - coordenadasY[1] ) ) );
double magnitudC = Math.sqrt( ( (coordenadasX[0] - coordenadasX[2] ) * ( coordenadasX[0]- coordenadasX[2] ) ) + ( ( coordenadasY[0] - coordenadasY[2] ) * ( coordenadasY[0] - coordenadasY[2] ) ) );
double perimetroTriangulo = magnitudA + magnitudB + magnitudC;
double incentroNumeradorx = ((coordenadasX[0]*magnitudB)+(coordenadasX[1]*magnitudC)+(coordenadasX[2]*magnitudA));
double incentroNumeradory = ((coordenadasY[0]*magnitudB)+(coordenadasY[1]*magnitudC)+(coordenadasY[2]*magnitudA));
double incentroX = incentroNumeradorx/perimetroTriangulo;
double incentroY = incentroNumeradory/perimetroTriangulo;
Point2D.Double incentroPunto = new Point2D.Double(incentroX,incentroY);
double magnitudD = Math.sqrt( ( (incentroX - coordenadasX[3] ) * ( incentroX- coordenadasX[3] ) ) + ( ( incentroY - coordenadasY[3] ) * ( incentroY - coordenadasY[3] ) ) );
Line2D.Double centroaLin = new Line2D.Double(incentroPunto, click1Pun);
Line2D.Double centrobLin = new Line2D.Double(incentroPunto, click2Pun);
Line2D.Double centrocLin = new Line2D.Double(incentroPunto, click3Pun);
double magPerpendicularX = coordenadasX[1] - coordenadasX[0];
double magPerpendicularY = coordenadasY[1] - coordenadasY[0];
double magnitudBase = sqrt(magPerpendicularX*magPerpendicularX + magPerpendicularY*magPerpendicularY);
magPerpendicularX /= magnitudBase;
magPerpendicularY /= magnitudBase ;
double lambda = (magPerpendicularX * (incentroX - coordenadasX[0])) + (magPerpendicularY * (incentroY - coordenadasY[0]));
double x4 = (magPerpendicularX * lambda) + coordenadasX[0];
double y4 = (magPerpendicularY * lambda) + coordenadasY[0];
Point2D.Double pointPerpendicular= new Point2D.Double(x4,y4);
Line2D.Double ladoH = new Line2D.Double(incentroPunto, pointPerpendicular);
double magnitudRadioincentro= Math.sqrt( ( (x4 - incentroX ) * ( x4- incentroX ) ) + ( ( y4 - incentroY ) * ( y4 - incentroY ) ) );
int magnitudDiametroincentro = (int)magnitudRadioincentro *2;
int x = (int)incentroX;
int y = (int)incentroY;
int x2 = (int)incentroX;
int y2 = (int)incentroY;
x = x-(magnitudDiametroincentro/2);
y = y-(magnitudDiametroincentro/2);
double magnitudRadiocircuncentro = (magnitudA*magnitudB*magnitudC)/
Math.sqrt((magnitudA+magnitudB+magnitudC)*
((magnitudB+magnitudC)-magnitudA)*
((magnitudC+magnitudA)-magnitudB)*
((magnitudA+magnitudB)-magnitudC));
int magnitudDiametrocircuncentro= (int)magnitudRadiocircuncentro *2;
x2 = x2-(magnitudDiametrocircuncentro/2);
y2 = y2-(magnitudDiametrocircuncentro/2);
g.setColor(Color.GREEN);
g.drawOval(x,y,magnitudDiametroincentro,magnitudDiametroincentro);
g.setColor(Color.GREEN);
g.drawOval(x2,y2,magnitudDiametrocircuncentro,magnitudDiametrocircuncentro);
double angle = (double) Math.toDegrees(Math.atan2( coordenadasX[1] - coordenadasX[0], coordenadasY[1] - coordenadasY[0]));
if(angle < 0){
angle += 360;
}
System.out.println("***********" + angle);
g.setColor(Color.BLACK);
g2.draw(ladoaLin);
g2.draw(ladobLin);
g2.draw(ladocLin);
g.setColor(Color.BLUE);
g2.draw(centroaLin);
g2.draw(centrobLin);
g2.draw(centrocLin);
g.setColor(Color.RED);
g2.draw(ladoH);
}
}
最佳答案
给定三个点,它们的坐标是:
(p,t)(q,u)(s,z)
...由这三个点定义的圆的方程为:
x ^ 2 + y ^ 2 + Ax + By + C = 0
哪里:
```
A=((u-t)*z^2+(-u^2+t^2-q^2+p^2)*z+t*u^2+(-t^2+s^2-p^2)*u+(q^2-s^2)*t)/((q-p)*z+(p-s)*u+(s-q)*t)
B=-((q-p)*z^2+(p-s)*u^2+(s-q)*t^2+(q-p)*s^2+(p^2- q^2)*s+p*q^2-p^2*q)/((q-p)*z+(p-s)*u+(s-q)*t)
C=-((p*u-q*t)*z^2+(-p*u^2+q*t^2-p*q^2+p^2*q)*z+s*t*u^2+(-s*t^2+p*s^2-p^2*s)*u+(q^2*s-q*s^2)*t)/((q-p)*z+(p-s)*u+(s-q)*t)
``
以上是一般解决方案。您可以将A,B和C的公式放入程序中,并找到任意圆的方程式(给定3点)。
对于点(0,1)(1,0)(0,-1)的特定问题,您将获得:
A = 0
B = 0
C = -1
...所以等式为
x ^ 2 + y ^ 2 -1 = 0(单位圆
关于java - 如何通过计算外接圆心绘制给定三角形的外接圆,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50093739/