سؤال

I have to draw an EQ triangle, and the coordinates of two points are already given. I'm using the drawPolygon method, and so I have to find the third point out.

int xCoord[] = {x1, x2, ?};
int yCoord[] = {y1, y2, ?};
g.drawPolygon(xCoord, yCoord, 3);
هل كانت مفيدة؟

المحلول

Given the two initial points, and the fact that the height of an equilateral triangle with edge length a is a*sqrt(3)/2, you can compute

  • the edge between the given points
  • the (normalized) direction of the perpendicular to this edge
  • the center of this edge

Then the tip points of the equilateral triangle are given as

center +/- height * perpendicularDirection

This is implemented here in the computeTipPoint method:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;


public class EquilateralTriangleTest
{
    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                createAndShowGUI();
            }
        });
    }

    private static void createAndShowGUI()
    {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        EquilateralTrianglePanel panel = new EquilateralTrianglePanel();
        f.getContentPane().add(panel);

        f.setSize(1000,800);
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }
}

class EquilateralTrianglePanel extends JPanel implements MouseMotionListener
{
    private final Point2D point0;
    private final Point2D point1;

    EquilateralTrianglePanel()
    {
        this.point0 = new Point2D.Double(600,500);
        this.point1 = new Point2D.Double(400,600);
        addMouseMotionListener(this);
    }

    @Override
    protected void paintComponent(Graphics gr)
    {
        super.paintComponent(gr);
        Graphics2D g = (Graphics2D)gr;

        g.setColor(Color.BLACK);
        g.draw(new Line2D.Double(point0, point1));

        g.setColor(Color.RED);
        draw(g, point0);
        draw(g, point1);

        int x0 = (int)point0.getX();
        int y0 = (int)point0.getY();
        int x1 = (int)point1.getX();
        int y1 = (int)point1.getY();

        Point2D point2a = computeTipPoint(point0, point1, true);
        int x2a = (int)point2a.getX();
        int y2a = (int)point2a.getY();

        g.setColor(Color.BLUE);
        int xCoorda[] = {x0, x1, x2a};
        int yCoorda[] = {y0, y1, y2a};
        g.drawPolygon(xCoorda, yCoorda, 3);

        Point2D point2b = computeTipPoint(point0, point1, false);
        int x2b = (int)point2b.getX();
        int y2b = (int)point2b.getY();

        g.setColor(Color.MAGENTA);
        int xCoordb[] = {x0, x1, x2b};
        int yCoordb[] = {y0, y1, y2b};
        g.drawPolygon(xCoordb, yCoordb, 3);
    }

    private static void draw(Graphics2D g, Point2D p)
    {
        Ellipse2D e = new Ellipse2D.Double(
            p.getX()-3, p.getY()-3, 6, 6);
        g.fill(e);
    }


    @Override
    public void mouseDragged(MouseEvent e)
    {
        point0.setLocation(e.getPoint());
        repaint();
    }

    @Override
    public void mouseMoved(MouseEvent e)
    {
        point1.setLocation(e.getPoint());
        repaint();
    }

    private static Point2D computeTipPoint(
        Point2D p0, Point2D p1, boolean right)
    {
        double dx = p1.getX() - p0.getX();
        double dy = p1.getY() - p0.getY();
        double length = Math.sqrt(dx*dx+dy*dy);
        double dirX = dx / length;
        double dirY = dy / length;
        double height = Math.sqrt(3)/2 * length;
        double cx = p0.getX() + dx * 0.5;
        double cy = p0.getY() + dy * 0.5;
        double pDirX = -dirY;
        double pDirY = dirX;
        double rx = 0;
        double ry = 0;
        if (right)
        {
            rx = cx + height * pDirX;
            ry = cy + height * pDirY;
        }
        else
        {
            rx = cx - height * pDirX;
            ry = cy - height * pDirY;
        }
        return new Point2D.Double(rx, ry);
    }
}

نصائح أخرى

You have to solve following system of equations to find coordinates of the third vertex:

(x-x1)^2 + (y-y1)^2 = R^2
(x-x2)^2 + (y-y2)^2 = R^2

where

R = dist(x1, y1, x2, y2)
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top