This web is incomplete. We are working on it!!'''''

Rotate Model Tool

How does this tool work?

This tool us will help to we can rotate the model in the reference point created by the fixed elements (Fixed Elements Tool).

The reference point about the model will rotate, is established depending the fixed elements:

  • If the fixed elements are two Points --> The reference point of the rotation, is the middle point of the segment that unite the two points.
  • If the fixed elements are two Lines (or Segments) --> The reference point of the rotation, is the intersection of the lines (or segments). If the lines are parallels, the reference point is the coordinates center.
  • If the fixed elements are one Point and one Line --> The reference point of the rotation, is the middle point of the minimun segment that unite the point with the line.

This explanation is divided into two parts for easy monitoring. The first part will explain the concept of rotation, and as we get moving. The second part explain about the visualization of the reference point of rotation and the functionality to move it where we want.

Implementation I: The Rotation

Class Variables

There are two variables, one object of the PointXY class, called origin, and one object of the VextorXY class, called lastMove.

PointXY origin;
VectorXY lastMove;

Public Methods

Then we explain the algorithms of the functions in pseudocode.

mouseBeginDrag(MouseAutomataDragEvent mae)

if secondaryButton.isPressed() then return endif;

VectorXY startVector = new VectorXY(origin,start); 
      
lastMove = VectorXY(origin,end);  
                     
Double alpha = startVector.angle(lastMove);

if (┬ČlastMove.isZero()) then       
    Matrix m = new Matrix(origin, alpha);
    editor.update(this,m);
endif
  1. Obtain the first click previous the drag, in the start variable.
  2. Obtain the vector between the start point and reference point called origin.
  3. Obtain the vector between the reference point and the first point dragged called end.
  4. Obtain the angle between the previous vectors, and now we know the first angle rotate.
  5. Update the editor with the new matrix rotation.

mouseDrag(MouseAutomataDragEvent mae) and mouseEndDrag(MouseAutomataDragEvent mae)

This method is very similar to previous:

  1. Obtain the vector between the reference point and the new point dragged called end.
  2. Obtain the angle between the previous vectors, and now we know the new angle rotate.
  3. Update the editor with the new matrix rotation.

changeRotatePoint(PointXY pxy)

This method change the reference point of the rotate, when we have changed the fixed elements.

public void changeRotatePoint(PointXY pxy)

This tool is enabled for the new GUI UserProblemEditor, and must exists communication with the Editor. When two elements are fixed, the Editor send to the RotateTool the new reference point, because the RotateTool has the next function: public void changeRotatePoint(PointXY pxy).

Implementation II: The Visualization and Moving of the Reference Point

When this tool is active, we visualize in screen a benchmark so that we know the rotation:

We will rotate with the mouse if you hold the left button pressed, and a change of position in the reference point if we drag it pressing the right mouse button:

Class Variables

There are two Color variables, to help the color change that will suffer the reference point depending on whether you are moving or not.

Color pink;
Color red;

Public Methods

Then we explain the algorithms of the functions in pseudocode.

The Drag Functions

In these functions, we add the following code to see if we pressed the right mouse button and if the cursor is within the area occupied by the reference point. Then, we'll make a color change of the reference point and get the current cursor to change the reference point. This information is sent to Canvas2D class to be painted.

This is the added code:

UserCanvas2D c = (UserCanvas2D)editor.getCanvas2D();
PointXY moveRef = c.getPoint(mae.getCursor());
      
if (mae.getButton() == secondaryButton){
   if (isPointReferenceArea(moveRef)){
      origin=moveRef;
      c.setRotateReferencePoint(origin);
      c.setPointReferenceColor(red);
   }
   else c.setPointReferenceColor(pink);
   return;
}
c.setPointReferenceColor(pink);

The functions setPointReferenceColor(Color c) and setRotateReferencePoint(PointXY p) are the key to communication with the Canvas2D.

Canvas2D Modifications

In the repaint(boolean b) function from Canvas2D, we have added a new boolean to know if we have the rotate tool enabled. In the repaint(boolean b) function from Canvas2D, we have added a new boolean to know if we have the rotate tool enabled, then Canvas2D will paint the reference point in the repaint() function, with this code added:

if (rotateReference){ 
   g2D.setColor(pointReferenceColor);
   Pixel p = getPixel(pointReference);
   int size = 15;
   g2D.drawOval(p.getX()-size/2,p.getY()-size2,size,size);
   size 5;
   g2D.fill(new Ellipse2D.Double(p.getX()-size/2,p.getY()-size/2,size,size));              
}

Attachments