Size of the drawing Logic fail
in
Programming Questions
•
1 month ago
Hi,
Here I have written a code which should detect the size of the shape drawn on the screen and draw rectangle around it but the problem is it only saves the min value of the (x,y) coordinates of the shape and doesn't save the max value of coordinates instead it saves the last coordinates of the mouse.
Any help ?
- int flag=0;
- ArrayList poop;
- PVector PVprep, SaveMax, SaveMin;
- void setup()
- {
- size(400, 400);
- smooth();
- poop = new ArrayList();
- PVprep = new PVector(0, 0);
- SaveMax = new PVector(0, 0);
- SaveMin = new PVector(0, 0);
- }
- void draw()
- {
- background(255);
- for (int i=0;i<poop.size();i++)
- {
- PVector PV = (PVector) poop.get(i);
- stroke(0);
- line(PV.x, PV.y, PVprep.x, PVprep.y);
- if (PV.x > SaveMax.x)
- {
- SaveMax.x = PV.x;
- }
- if (PV.x < SaveMax.x)
- {
- SaveMin.x = PV.x;
- }
- if (PV.y > SaveMax.y)
- {
- SaveMax.y = PV.y;
- }
- else if (PV.y < SaveMax.y)
- {
- SaveMin.y = PV.y;
- }
- PVprep = PV;
- }
- if (flag==1)
- {
- noFill();
- stroke(255, 0, 0);
- rect(SaveMin.x, SaveMin.y, abs(SaveMin.x - SaveMax.x), abs(SaveMin.y-SaveMax.y));
- }
- println( "SaveMin.x " + SaveMin.x + " SaveMin.y " + SaveMin.y + " SaveMax.x " + SaveMax.x + " SaveMax.y " + SaveMax.y );
- }
- void mouseDragged()
- {
- PVector P = new PVector(mouseX, mouseY);
- poop.add(P);
- flag=0;
- }
- void mouseReleased()
- {
- flag=1;
- }
2