We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › zoom in and out, help please!
Page Index Toggle Pages: 1
zoom in and out, help please! (Read 631 times)
zoom in and out, help please!
Mar 17th, 2009, 8:20pm
 
Hey guys,
I had to create a joystick and 2 buttons that would control google earth. Since it needs python our teacher said that to simulate google earth on processing. Put a picture from google earth and show the movement we want to do.
So I had my joystick working to do left right up and down.
I got two digital buttons which I want them to make the zoom in and out.
I couldn't figure it out, really, I'm a beginner...
I found on the reference of processing the scale code, didn t work.
I ll post my code and u guys tell me what is wrong

thanks

Sonia

//****************Arduino***********************
/*
*  Sample code to send multiple analog inputs to Processing
*  from Arduino  
*/

int analogInput1 = 0;  
int analogInput2 = 1;
int digitalInput3 = 2;
int digitalInput4 = 3;
int ledPin = 13;

int val1 = 0;  
int val2 = 0;
int val3 = 0;
int val4 = 0;

// an identifier for each value
char label1 = 'A';  
char label2 = 'B';  


void setup() {  
Serial.begin(9600);  //setup the serial port
}

void loop()
{
  val1 = analogRead(analogInput1);  //read pins ito variables
  val2 = analogRead(analogInput2);
  val3 = digitalRead(digitalInput3);
  val4 = digitalRead(digitalInput4);
 
  Serial.print (label1, BYTE);
  Serial.print (val1, DEC);  
  Serial.print (10, BYTE);
 
  Serial.print(label2, BYTE);
  Serial.print (val2, DEC);  
  Serial.print (10, BYTE);
 

 
 
 delay(300);   // pause to prevent comm errors

 if (val3 & val4 == HIGH) {
digitalWrite(ledPin, LOW);
}else{
digitalWrite(ledPin, HIGH);
delay(200);
digitalWrite(ledPin, LOW);
delay(300);
}

}

_________________________________________________________



//**************** PROCESSING****************
/*
*  Processing code to receive multiple analog sensor readings
*  from Arduino
*  Uses string buffers to hold values larger than 0-255 (byte size)
*/

import processing.serial.*;

Serial port;

int val;           //holds serial line data to send to parser
String bufA="";
String bufB="";  //buffer strings for each value
int inputFlag;  // switches between sensor data for as many sensors as used
int val1;        //global variables to hold the analog sensor values
int val2;
int val3;
int val4;
PImage b;


//float xCoordinate = 0;
//float yCoordinate = 0;

void setup(){
size(799,799);
port = new Serial(this,Serial.list()[0],9600); //open serial port
stroke(0);
strokeWeight(4);
b = loadImage("main.png");
}

void draw (){
if(port.available() > 0){
  val = port.read();
  serialEvent(val);    // send the port.read data to the serial parser function
     background(0);
 image(b, (val1/3-700),(val2/3-500));

/* if(val3==390) {

 image(b, (val1/3),(val2/3));
}
else {
 image(b, (val1/3-700),(val2/3-500));//this the image of google earth
//lower the code that I canceled is the one for zoomin that I tried.    
}*/

  //image(b, (val3/3-700),(val4/3-500));
 //ellipse (val1/3, val2/3, 30, 30);
 //smooth();
//line(20,40,80,40);
  //print ("A = " );
  //println ( val1 );
  //print ("B = " );
  //println ( val2 );
}

}

void serialEvent(int serial){
if(serial!=10) {    // 10 is the line end sent from Arduino  
  if (serial=='A') { //check that buffer is at initial value
    inputFlag = 1;
  }
  if (serial=='B') {
    inputFlag = 2;
  }
 
  if (inputFlag == 1){ // fill data into buffer string
    if (serial!='A') {
      bufA += char(serial);
 
    }
  }
  if (inputFlag == 2){
    if (serial!='B') {  // fill data into buffer string
      bufB+= char(serial);
    }
  }
}
else {
  if (inputFlag == 1){  
    val1 = int(bufA); // load data into variable from the buffer
    bufA="";           //clear the buffer
  }
  if (inputFlag == 2) {  
    val2 = int(bufB); // load data into variable from the buffer
    bufB="";            // clear the buffer
  }
}  

}
Re: zoom in and out, help please!
Reply #1 - Mar 17th, 2009, 9:25pm
 
soniamcangel wrote on Mar 17th, 2009, 8:20pm:
I got two digital buttons which I want them to make the zoom in and out.


To get a zoom effect for pictures you can simply draw the orignal image to a larger size.
So, if the image has 500x500 - then draw it to 1000x1000 and you'll get a 2x zoom effect.
Re: zoom in and out, help please!
Reply #2 - Mar 17th, 2009, 10:36pm
 
Alternately, (and perhaps cleaner, from a design perspective), put a call to scale() in your draw() routine before you call image() to put the image on the screen.

Create a global "displayScale" variable, which you'll initially set to 1.0.  You'll have to hook up the serial event handlers so that your buttons cause that variable to multiply or divide by some constant amount (1.1 is probably a good amount; larger amounts will zoom in/out faster, values closer to 1.0 will go slower).

Note, you can achieve left/right/up/down panning by either drawing the image at coordinates that you change, depending on the joystick input, OR by using joystick input to control the translation of the entire Processing coordinate space.  That way, you would always draw your image at _logical_ coordinates (0,0), but by having called translate() and scale() appropriately ahead of time, Processing will take care of the actual image manipulation for you.
Page Index Toggle Pages: 1