mouse mapping problems
in
Programming Questions
•
2 years ago
hi,
in attached code, the segments follow the mouse pointer when the ellipse is in centre. However, when ellipse is dragged off-centre, the mouse pointer controls segments at a great distance.. how can i fix this so that segments precisely follow mouse pointer wherever it is on screen?
- float[][] ang;
- int ind;
- boolean over = false;
- boolean selected = false;
- int posX, posY;
- int offsetX, offsetY;
- float radius = 20.0f;
- void setup()
- {
- size(512, 512);
- ang = new float[][]
- {
- {
- 0, HALF_PI
- }
- ,
- {
- HALF_PI, PI
- }
- ,
- {
- PI+HALF_PI, TWO_PI
- }
- ,
- {
- PI, PI+HALF_PI
- }
- };
- posX = width/2;
- posY = height/2;
- smooth();
- }
- void draw()
- {
- background(0, 0, 255);
- ind = 0;
- float mx = mouseX-width*.5;
- float my = mouseY-height*.5;
- background(0, 0, 255);
- strokeWeight (0.3);
- stroke(255, 0, 0, 240);
- int tx=mouseX;
- int ty=mouseY;
- line(posX, 0, posX, height);
- line(0, posY, width, posY);
- ellipse (posX, posY, radius*2.0f, radius*2.0f);
- strokeWeight(2);
- if (my < 0) ind+=2;
- if (mx < 0) ind++;
- float rad = dist(0, 0, mx, my)*2;
- //fill (clrs[ind]);
- arc(posX, posY, rad, rad, ang[ind][0], ang[ind][1]);
- if (selected)
- strokeWeight (2.0f);
- fill (255, 0, 0, 190);
- ellipse (posX, posY, radius*2.0f, radius*2.0f);
- }
- void mouseMoved() {
- over = false;
- if (sqrt(sq(mouseX-posX)+sq(mouseY-posY))<=radius) {
- over = true;
- }
- }
- void mousePressed() {
- if (over) {
- selected = true;
- offsetX = posX-mouseX;
- offsetY = posY-mouseY;
- }
- }
- void mouseReleased() {
- selected = false;
- }
- void mouseDragged()
- {
- if (selected) {
- posX = max(0, min(mouseX+offsetX, width));
- posY = max(0, min(mouseY+offsetY, height));
- }
- }
1