We are about to switch to a new forum software. Until then we have removed the registration on this forum.
hi everyone! I'm coding a simple 2D CAD, the main problem is getting the Cartesian coordinate of the mouse pointer in the local cartesian system after a translation and/or a scale. I've done this and it works well for translation but not for scale :(
public class Plane3D extends PApplet {
protected boolean mouse_in_out = false;
@ Override
public void setup(){
background(255);
addMouseWheelListener(new java.awt.event.MouseWheelListener() {
@ Override
public void mouseWheelMoved(java.awt.event.MouseWheelEvent evt) {
mouseWheel(evt.getWheelRotation());
}});
this.addMouseListener(new MouseListener() {
@ Override
public void mouseClicked(MouseEvent me) {}
@ Override
public void mousePressed(MouseEvent me) {
System.out.println("Qui siamo");
X_start = mouseX;
Y_start = mouseY;
}
@ Override
public void mouseReleased(MouseEvent me) {
System.out.println("Rilasciato");
float dx = mouseX-X_start;
float dy = mouseY-Y_start;
System.out.println("dx:" + dx + "dy: " +dy );
XO_coord += dx;
YO_coord += dy;
}
@ Override
public void mouseEntered(MouseEvent me) {
mouse_in_out=true;
}
@ Override
public void mouseExited(MouseEvent me) {mouse_in_out=false;}
});
}
@ Override
public void draw(){
background(255);
scaleUp = wheel;
transScale(mouseX, mouseY, scaleUp);
};
//---- Coordinate System ----
private float X_start,Y_start;
public float X_coord;
public float Y_coord;
public float XO_coord;
public float YO_coord;
//---- Translation ----
private float scaleUp = 0.5f;
private float wheel = 1.0f;
private float wheelMult = 0.10f;
private float xOffset;
private float yOffset;
private float xold;
private float yold;
private float xpiv;
private float xpivOld;
private float ypiv;
private float ypivOld;
private void mouseWheel(int step) {
wheel=constrain((float)wheel+step*wheelMult*-1,0.1f,100f);
}
protected void transScale(float x, float y,float scale) {
if (mousePressed == true) {
xOffset = x-xold;
yOffset = y-yold;
xpiv = (xpivOld-xOffset/scaleUp);
ypiv = (ypivOld-yOffset/scaleUp);
xpiv = xpiv + (xOffset/scaleUp);
ypiv = ypiv + (yOffset/scaleUp);
xpivOld = xpiv;
ypivOld = ypiv;
xold = x;
yold = y;
}
else{
xOffset = x-xold;
yOffset = y-yold;
xpiv = (xpivOld-xOffset/scaleUp);
xpivOld = xpiv;
ypiv = (ypivOld-yOffset/scaleUp);
ypivOld = ypiv;
xold = x;
yold = y;
}
X_coord = (mouseX - XO_coord*scale)/scale;
Y_coord = (mouseY - YO_coord*scale)/scale;
// blue grid position
translate(x, y);
scale(scale);
// negate mouse position
translate(xpiv, ypiv);
//System.out.println("X:" + X_coord + "Y:" + Y_coord);
System.out.println("X:" + modelX(mouseX,mouseY,0) + "Y:" + modelY(mouseX,mouseY,0));
}
any ideas?