最初に把握する必要があるのは、各コーナーの位置です。あなたはそれぞれの辺の長さを持っているので、余弦定理を使うことができます...

...サイド1(a)とサイド2(b)の間の角度を取得するには:

三角形の角の位置は次のとおりです。
- a)[0、0]
- b)[b、0]
- c)[c * cos(angle)、c * sin(angle)]
この後、三角形の中心に描画したいので、間違った場所から三角形が作成されます。三角形の中心の計算はさまざまな方法で行うことができますが、これは非常に単純な方法です。
centerX = (a.x + b.x + c.x) / 3
centerY = (a.y + b.y + c.y) / 3
次に、そのポイントを選択したポイントに変換できます。
これがあなたが望むことをするいくつかのコードです:
static class Triangle {
double a, b, c;
public Triangle(double a, double b, double c) {
this.a = a;
this.b = b;
this.c = c;
}
public double aAngle() {
return Math.acos(-(Math.pow(a, 2) - Math.pow(b, 2) - Math.pow(c, 2)) / (2 * b * c));
}
public Point[] triangle() {
double angle = aAngle();
Point[] p = new Point[3];
p[0] = new Point(0, 0);
p[1] = new Point((int) b, 0);
p[2] = new Point((int) (Math.cos(angle) * c), (int) (Math.sin(angle) * c));
Point center = new Point((p[0].x + p[1].x + p[2].x) / 3,
(p[0].y + p[1].y + p[2].y) / 3);
for (Point a : p)
a.translate(-center.x, -center.y);
return p;
}
}
使用例:
public static void main(String[] args) {
final Triangle t = new Triangle(20, 30, 40);
final Point translation = new Point(100, 400);
JFrame frame = new JFrame("Test");
frame.add(new JComponent() {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Point[] p = t.triangle();
g.translate(translation.x, translation.y);
for (int i = 0; i < p.length; i++)
g.drawLine(p[i].x,
p[i].y,
p[(i+1) % p.length].x,
p[(i+1) % p.length].y);
}
});
frame.setSize(800, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}