How to rotate a line based on a given number of degrees

纵饮孤独 提交于 2019-12-13 02:15:20

问题


I have a line drawn with a Graphics object. I want to rotate this line a certain amount of degrees based on how much the mouse is dragged. I can get the number of degrees i need to rotate it but how do i then rotate the line based on that?

Thank You!


回答1:


You can create a Line2D object for your original line. Then you can use AffineTransform#getRotateInstance to obtain an AffineTransform that does the rotation about a certain angle, around a certain point. Using this AffineTransform, you can create a rotated Line2D object to paint. So your painting code could roughly look like this:

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

    // Create the original line, starting at the origin,
    // and extending along the x-axis
    Line2D line = new Line2D.Double(0,0,100,0);

    // Obtain an AffineTransform that describes a rotation
    // about a certain angle (given in radians!), around
    // the start point of the line. (Here, this is the
    // origin, so this could be simplified. But in this
    // form, it's more generic)
    AffineTransform at = 
        AffineTransform.getRotateInstance(
            Math.toRadians(angleInDegrees), line.getX1(), line.getY1());

    // Draw the rotated line
    g.draw(at.createTransformedShape(line));
}



回答2:


Alright, you will need to compute the length of the line, assuming that the ends of the line are (x0,y0) and (x1,y1), and (x,y) are the mouse coordinates, what you want is the point (x2,y2) that's on the line between (x0,y0) and (x,y), the distance between (x0,y0) and (x2,y2) must be the same as the one between (x0,y0) and (x1,y1).

The distance between (x0,y0) and (x1,y1) is:

double dx = x1-x0;
double dy = y1-y0;
double length = Math.sqrt(dx*dx, dy*dy);

The distance between (x0,y0) and (x,y) is:

double dx1 = x-x0;
double dy1 = y-y0;
double mouseDist = Math.sqrt(dx1*dx1, dy1*dy1);

And (x2,y2) are:

int x2 = x0 + (int)(dx1*length/mouseDist);
int y2 = y0 + (int)(dy1*length/mouseDist);



回答3:


I suppose you are talking about Java AWT Graphics class. Graphics can be thought of as a canvas. It's an array of pixel values and "drawing a line" is just a utility function that changes the values of some of these pixels - there is no "line object" to speak of, from it's point of view. Normally you should erase the whole thing and draw a new line with the angle you want. However for that, you may want to look at Graphics2D (http://docs.oracle.com/javase/7/docs/api/java/awt/Graphics2D.html) and in particular at setTransform and the AffineTransform class.




回答4:


static Point rotateLineClockWise(Point center, Point edge, int angle) {
    double xRot = (int) center.x + Math.cos(Math.toRadians(angle)) * (edge.x - center.x) - Math.sin(Math.toRadians(angle)) * (edge.y - center.y);
    double yRot = (int) center.y + Math.sin(Math.toRadians(angle)) * (edge.x - center.x) + Math.cos(Math.toRadians(angle)) * (edge.y - center.y);
    return new Point((int) xRot, (int) yRot);
}


来源:https://stackoverflow.com/questions/29881808/how-to-rotate-a-line-based-on-a-given-number-of-degrees

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!