Dan
Dan

Reputation: 7734

Rotate two parallel lines to create an X

The following code is the code that I am using to rotate two rectangles is below

Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g2d.setColor(Color.WHITE);

//r1
Rectangle2D r1 = new Rectangle2D.Double(0, 0, 50, 4);
g2d.rotate(Math.toRadians(45));
g2d.fill(r1);

//r3
Rectangle2D r3 = new Rectangle2D.Double(0, 25, 50, 4);
g2d.rotate(Math.toRadians(-90));
g2d.fill(r3);

This create something which looks like this

enter image description here

Whereas I am trying to create something which looks like this

enter image description here

This occurs since when the rectangles are rotated, they are both rotated around the point 0,0. To fix that I tried using rotate(double theta, double x, double y). However I am having trouble using that correctly. For example when I have tried

Rectangle2D r3 = new Rectangle2D.Double(0, 25, 50, 4);
g2d.rotate(Math.toRadians(-90), 25, 25);

or

Rectangle2D r3 = new Rectangle2D.Double(0, 25, 50, 4);
g2d.rotate(Math.toRadians(-90), 0, 25);

I get similar undesired results when both the rectangles were being rotated around the point 0,0. I would appreciate any help if fixing my problem.

If you are wondering why I have done it like this, it is because I am hoping to make a effect similar to when you click on the 3 parallel lines seen here by the time I finish coding the graphic

Upvotes: 2

Views: 188

Answers (2)

Dan
Dan

Reputation: 7734

So it turns out that this can be done with some relatively simple maths. As the shape I am trying to make is a perfect X.

To work out the position for the rectangle we can use Pythagorean theorem.

enter image description here

The image above shows two steps.

Translation [2√2, 0] from the point [0, 0]
Rotate 45deg from the point [2√2, 0]

Next we need to work out the minimum point of this rectangle. Again we can use Pythagorean theorem.

enter image description here

This tells us where the top point of the second rectangle will be

Difference in height: 4 - 2√2
Bottom of line when straight: [0, 27√2 + (4 - 2√2)] = [0, 4 + 25√2]
Top of line when straight: [0, 25√2]

Finally we can put in the last rectangle starting at [0, 0]

Translation [0, 25√2] from the point [0, 0]
Rotate -45deg from the point [0, 25√2]

Now that the theory is out of the way, what does this look like in code? It looks similar to the code below

//Values
final static double[] r1Points = {2.828427125, 0}; //Equivilant 2√2
final static double[] r3Points = {0, 35.35533906}; //Equivilant 25√2
final static int[] widthNHeight = {50, 4}; //Width then height
final static double angle = 45.0; //Angle to rotate lines

//Declaring the rectangles
Rectangle2D r1 = new Rectangle2D.Double(r1Points[0], r1Points[1], widthNHeight[0], widthNHeight[1]);
Rectangle2D r3 = new Rectangle2D.Double(r3Points[0], r3Points[1], widthNHeight[0], widthNHeight[1]);

//r1
g2d.rotate(Math.toRadians(angle), r1Points[0], r1Points[1]); //Rotates graphic for first rectangle
g2d.fill(r1);

//r3
g2d.rotate(Math.toRadians(-angle), r1Points[0], r1Points[1]); //Rotates the graphic back to straight
g2d.rotate(Math.toRadians(-angle), r3Points[0], r3Points[1]); //Rotates graphic for second rectangle
g2d.fill(r3);

Upvotes: 0

Coderino Javarino
Coderino Javarino

Reputation: 2881

package test;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;

import javax.swing.*;

public class Cross extends JPanel {

    private Rectangle2D rectangle;

    Cross() {
        rectangle = new Rectangle2D.Double(0, 0, 50, 4);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        g2.setColor(Color.red);
        g2.fillRect(0, 0, getWidth(), getHeight());

        g2.setColor(Color.white);
        AffineTransform at = g2.getTransform();
        g2.translate(5, 5);
        g2.rotate(Math.toRadians(45));
        g2.fill(rectangle);

        g2.setTransform(at);
        g2.translate(5, 5 + Math.sqrt(2) * 25);
        g2.rotate(Math.toRadians(-45));
        g2.fill(rectangle);

        g2.setTransform(at);
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("Cross");
        frame.add(new Cross());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(128, 128);
        frame.setVisible(true);
    }



}

Although I think I might have done mistake somewhere with maths (it looks somewhat odd), this should give you an idea.

Upvotes: 2

Related Questions