Java - calculating and placing the corner of a geometric shape

I am currently working on a program that allows the user to draw various geometric shapes. However, I am having some trouble calculating and placing corner objects in my panel Canvas

. The corner object is basically an extension of the object Arc2D

, which provides an additional method called computeStartAndExtent()

. Inside my class, Angle

this method calculates and finds the required launch and extension angle values:

private void computeStartAndExtent()
    {
        double ang1 = Math.toDegrees(Math.atan2(b1.getY2() - b1.getY1(), b1.getX2() - b1.getX1()));
        double ang2 = Math.toDegrees(Math.atan2(b2.getY2() - b2.getY1(), b2.getX2() - b2.getX1()));

        if(ang2 < ang1)
        {
            start = Math.abs(180 - ang2);
            extent = ang1 - ang2;
        }
        else
        {
            start = Math.abs(180 - ang1);
            extent = ang2 - ang1;
        }
        start -= extent;
    }

      

This is an error code that only works when connecting two strings to each other, however, when I connect the third to create a triangle, the result looks like this:

enter image description here

As you can see, the ADB corner is the only one that is set correctly. I couldn't figure out how to get over this. If you need more information / code, please let me know.

EDIT: b1 and b2 are Line2D objects in the method computeStartAndExtent()

.

Thank.

+3


source to share


1 answer


There are several things you can do to simplify the calculation:

  • Keep the vertices in order so it is always clear how to calculate the vertex angles pointing away from the corner
  • Also, always draw the polygon in one direction; then you can always draw corners in one direction. The example below assumes that the polygon is drawn clockwise. The same calculation of the angle will result in the outward arcs having the given polygon facing counterclockwise.

Sample code; not exactly the same as yours as I don't have your code, but has similar functionality:



import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.geom.Arc2D;

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

public class Polygon extends JPanel {
    private static final int RADIUS = 20;

    private final int[] xpoints = {
            10, 150, 80, 60
    };
    private final int[] ypoints = {
            10, 10, 150, 60
    };
    final Arc2D[] arcs;

    Polygon() {
        arcs = new Arc2D[xpoints.length];
        for (int i = 0; i < arcs.length; i++) {
            // Indices of previous and next corners
            int prev = (i + arcs.length - 1) % arcs.length;
            int next = (i + arcs.length + 1) % arcs.length;
            // angles of sides, pointing outwards from the corner
            double ang1 = Math.toDegrees(Math.atan2(-(ypoints[prev] - ypoints[i]), xpoints[prev] - xpoints[i]));
            double ang2 = Math.toDegrees(Math.atan2(-(ypoints[next] - ypoints[i]), xpoints[next] - xpoints[i]));
            int start = (int) ang1;
            int extent = (int) (ang2 - ang1);
            // always draw to positive direction, limit the angle <= 360
            extent = (extent + 360) % 360;
            arcs[i] = new Arc2D.Float(xpoints[i] - RADIUS, ypoints[i] - RADIUS, 2 * RADIUS, 2 * RADIUS, start, extent, Arc2D.OPEN);
        }
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(160, 160);
    }

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

        g.drawPolygon(xpoints, ypoints, xpoints.length);
        Graphics2D g2d = (Graphics2D) g;
        for (Shape s : arcs) {
            g2d.draw(s);
        }
    }

    public static void main(String args[]){
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame("Polygon");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new Polygon());
                frame.pack();
                frame.setVisible(true);
            }
        });
    }
}

      

Results in:

Screenshot of a polygon

+1


source







All Articles