This discussion is archived
1 Reply Latest reply: Jun 6, 2012 7:15 PM by KevinPas RSS

Rotating a Line Around a Point

896448 Newbie
Currently Being Moderated
Hi everybody,
I'm trying to rotate a line around I've been trying to rotate a line around a point for a few days already without success. Please help.
I have a line represented by starting and ending x and y, angel (>0) with axis x in degrees and height. I'm trying to rotate it by angel "angel" around a point.
This is my final code which doesn't work:
    private double xCoordStart = 0, yCoordStart = 0;
    private double xCoordEnd = 0, yCoordEnd = 0;
    private double angel = 0;
    private double  height;

    public void rotateAroundPoint(int angel, int x, int y) {        
        double[] coords = getCoordinatesBy(Math.sqrt((x - xCoordEnd) * (x - xCoordEnd) + (y - yCoordEnd) * (y - yCoordEnd)),
                xCoordEnd, yCoordEnd, getAngelBy(xCoordEnd,yCoordEnd,x,y)+angel); 
        yCoordEnd = coords[1];
        xCoordEnd = coords[0];
        coords=getCoordinatesBy(Math.sqrt((x - xCoordStart) * (x - xCoordStart) + (y - yCoordStart) * (y - yCoordStart))
                ,xCoordStart, yCoordStart,getAngelBy(xCoordStart,yCoordStart, x, y) +angel);
        yCoordStart = coords[1];
        xCoordStart = coords[0];
        this.angel = getAngelBy(xCoordStart, yCoordStart, xCoordEnd, yCoordEnd);
        
    }

    private static double[] getCoordinatesBy(double height, double startX, double startY, double angel) {
        double[] coordinates = new double[2];
        coordinates[1] = startY + (height * Math.sin((Math.toRadians(angel))));
        coordinates[0] = startX - (height * Math.cos(Math.toRadians(angel)));
        return coordinates;
    }
    
    private double getAngelBy(double x1, double y1, double x2, double y2){
        double delta_x = x2 - x1;
        double delta_y = y1-y2;
        
        return (360+ Math.toDegrees(Math.atan2(delta_y, delta_x)))%360;  //returns a positive degree in degrees
        
    } 
Thanks ahead.

Edited by: Vitali.pom on May 20, 2012 6:02 AM

Edited by: EJP on 7/06/2012 12:13: fixed your incorrect spelling
  • 1. Re: Rotating a Line Around a Point
    KevinPas Explorer
    Currently Being Moderated
    This rotates a rectangle filled with a gradient. I know it works I don't know if it will work for you.
    public void paint ( Graphics g, int x, int y )
    {
        Graphics2D g2D = (Graphics2D) g;
    
        // Erase where the Rectangle was.
        if ( iPrevX != -1 )
        {
            g2D.setColor ( g2D.getBackground () );
            g2D.fillRect ( iPrevX, iPrevY, iWidth, iHeight );
        }
    
        // Draw the rectangle and rotate it.
        AffineTransform AF01 = g2D.getTransform ();
        {
            Paint P1 = g2D.getPaint ();
            {
                AffineTransform AF02    = new AffineTransform   ();
                                AF02    .concatenate            ( AF01 );
                                AF02    .rotate                 ( java.lang.Math.toRadians ( m_fTheirHeading ), x, y );
                g2D.transform ( AF02 );
    
                GradientPaint gp = new GradientPaint (  (float) x,
                                                        (float) y + ( iHeight / 2 ),
                                                        Color.yellow,
                                                        (float) x + iWidth,
                                                        (float) y + ( iHeight / 2 ),
                                                        Color.red );
    
                g2D.setPaint ( gp );
                g2D.fillRect (     x, y, iWidth, iHeight );
                g2D.setPaint ( P1 );
            }
    
            g2D.setTransform ( AF01 );
        }
    Edited by: KevinPas on Jun 6, 2012 5:56 PM

    Edited by: EJP on 7/06/2012 12:15: fixed spelling error

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points