Skip to content

Instantly share code, notes, and snippets.

@vivekanandRdhakane
Last active April 25, 2020 05:50
Show Gist options
  • Save vivekanandRdhakane/87f0063be3f3c436565657d01b9bbc45 to your computer and use it in GitHub Desktop.
Save vivekanandRdhakane/87f0063be3f3c436565657d01b9bbc45 to your computer and use it in GitHub Desktop.
//Processing Code for StokeMapak
//Auther: Vivekanand Dhakane
//Updated on: 25/4/20120
import controlP5.*;
import hypermedia.net.*;
ControlP5 cp5;
String textValue = "";
UDP udp; // define the UDP object
int i, w_height, w_width;
int x_start, y_start;
FloatList angles;
float scroll_diameter =2.5; // replace with your scroll wheel diameter in cm
float unit_step_screen = 10;
float calibration_mult = 1.083608;
float unit_step_actual =calibration_mult* scroll_diameter*PI/24;
float scale_x=1, scale_y =1;
boolean set_first =false, display_start =false;
int x_traslate, y_traslate;
float pixToCm=1.0;
String scale_unit ="cm",con_status="DisConnected";
float actual_scale=1;
DropdownList d1;
Textfield scale;
float perimeter;
float data_count,angle_Z_degree_offset,area;
boolean fwd=true;
void setup()
{
size(displayWidth, displayHeight);
background(0);
udp = new UDP( this, 4210 );
//udp.log( true ); // <-- printout the connection activity
udp.listen( true );
x_start = displayWidth/2;
y_start = displayHeight/2;
PFont font = createFont("arial", 20);
angles =new FloatList();
cp5 = new ControlP5(this);
cp5.addTextfield("workspace")
.setPosition(displayWidth*0.8, displayHeight*0.03)
.setSize(displayWidth/17, displayHeight/30)
.setFont(font)
//.setFocus(true)
.setColor(color(255, 0, 0))
;
scale= cp5.addTextfield("scale")
.setPosition(displayWidth*0.65, displayHeight*0.03)
.setSize(displayWidth/25, displayHeight/30)
.setFont(font)
.setLabel("")
//.setFocus(true)
.setColor(color(255, 0, 0))
;
d1 = cp5.addDropdownList("unit")
.setPosition(displayWidth*0.7, displayHeight*0.03)
.setSize(displayWidth/25, displayHeight/10)
;
d1.addItem("cm", 0);
d1.addItem("mtr", 1);
d1.addItem("km", 2);
d1.setDefaultValue(0.0);
angles.append( 3.14/2);
angles.append( 0);
angles.append( -3.14/2);
angles.append( -3*3.14/4);
}
void draw()
{
background(0);
// translate(width/2, height/2, 0);
//println("scale_x"+scale_x);
text_displays();
pushMatrix();
translate(x_traslate, y_traslate);
scale(scale_x, scale_y);
/*pushMatrix();
int new_origin_x =(int)(displayWidth-displayWidth/scale_x);
int new_origin_y =(int)(displayHeight-displayHeight/scale_y);
println("new_origin_x"+new_origin_x);
translate(new_origin_x,new_origin_y);
*/
polygon_dislplay();
scale(1, 1);
popMatrix();
cord();
set_first_point();
}
public void workspace(String theText) {
// automatically receives results from controller workspace
println("a textfield event for controller 'workspace' : "+theText);
w_width = parseInt(theText);
w_height =w_width * displayHeight/displayWidth;
pixToCm = (float)w_width/displayWidth;
float cmToPix = (float)displayWidth/w_width;
unit_step_screen = unit_step_actual*cmToPix;
println("unit_step_screen="+unit_step_screen);
}
void controlEvent(ControlEvent theEvent) {
// DropdownList is of type ControlGroup.
// A controlEvent will be triggered from inside the ControlGroup class.
// therefore you need to check the originator of the Event with
// if (theEvent.isGroup())
// to avoid an error message thrown by controlP5.
if (theEvent.isController()) {
println("event from controller : "+theEvent.getController().getValue()+" from "+theEvent.getController());
String control =theEvent.getController().toString();
print("control="+control+"<");
if (control.contains("unit") || control.contains("scale"))
{
String scale_value = scale.getText();
actual_scale = parseFloat(scale_value);
float value =theEvent.getController().getValue();
print("value " +value);
if (value == 0)
{
scale_unit ="cm";
} else if (value == 1)
{
print("meter");
scale_unit ="m";
} else if (value == 2)
{
scale_unit ="km";
}
}
}
}
void set_first_point()
{
if (set_first)
{
fill(#FFEB31);
textSize(15);
//upper arrow
line(mouseX, 0, mouseX, mouseY);
line(mouseX, 0, mouseX+10, 10);
line(mouseX, 0, mouseX-10, 10);
line(mouseX, mouseY, mouseX+10, mouseY-10);
line(mouseX, mouseY, mouseX-10, mouseY-10);
int upper_margin = (mouseY-0);
int upper_margin_cm = (int)(upper_margin *pixToCm);
text(upper_margin_cm+" cm", mouseX, upper_margin/2);
//lower arrow
line(mouseX, displayHeight, mouseX, mouseY);
line(mouseX, displayHeight, displayWidth-10, displayHeight-10);
line(mouseX, displayHeight, displayWidth+10, displayHeight-10);
line(mouseX, mouseY, mouseX+10, mouseY+10);
line(mouseX, mouseY, mouseX-10, mouseY+10);
int lower_margin = (displayHeight-mouseY);
int lower_margin_cm = (int)(lower_margin *pixToCm);
text(lower_margin_cm+" cm", mouseX, mouseY+lower_margin/2);
//LEFT arrow
line(0, mouseY, mouseX, mouseY);
int left_margin = (mouseX-0);
int left_margin_cm = (int)(left_margin *pixToCm);
text(left_margin_cm+" cm", left_margin/2, mouseY);
//RIGHT arrow
line(displayWidth, mouseY, mouseX, mouseY);
int right_margin = (displayWidth-mouseX);
int right_margin_cm = (int)(right_margin *pixToCm);
text(right_margin_cm+" cm", mouseX+right_margin/2, mouseY);
}
}
void polygon_dislplay()
{
float x_cord= x_start;
float y_cord= y_start;
float x1_cm = 0;
float y1_cm =0;
area=0;
///display_start_point
if (display_start)
{
circle(x_start, y_start, 5);
}
for (int j = 0; j< angles.size(); j++)
{
float cur_angle = angles.get(j);
float x2 = x_cord + unit_step_screen*cos(cur_angle);
float y2 = y_cord +unit_step_screen*sin(cur_angle);
////////////Area calculation
float x2_cm = x1_cm + unit_step_actual*cos(cur_angle);
float y2_cm = y1_cm + unit_step_actual*sin(cur_angle);
// println("x1_cm="+x1_cm+" y1_cm="+y1_cm);
area += (actual_scale*actual_scale)*(y1_cm + y2_cm)*(x2_cm-x1_cm)/2;
;
x1_cm = x2_cm;
y1_cm = y2_cm;
// println("x_cord:"+x_cord+" y_cord:"+y_cord+" x2:"+x2+" y2:"+y2+"angle"+cur_angle);
fill(255);
stroke(255);
line(x_cord, y_cord, x2, y2);
x_cord=x2;
y_cord=y2;
}
println("Area="+area+"\n");
}
void text_displays()
{
fill(255);
textSize(25);
stroke(255);
//text(w_width+"x"+w_height+" cm",displayWidth*0.9/scale_x,displayHeight*0.05/scale_x);
text(w_width+"x"+w_height+" cm", displayWidth*0.88, displayHeight*0.05);
textSize(15);
text("1 cm="+actual_scale+" "+scale_unit, displayWidth*0.65, displayHeight*0.1);
///rect for workspace
noFill();
rect(displayWidth*0.79, displayHeight*0.024, displayWidth*0.2, displayHeight*0.08);
textSize(15);
// text("+:ZoomIn \n -: ZoomOut",displayWidth*0.6/scale_x,displayHeight*0.05/scale_x);
text("+:ZoomIn \n -: ZoomOut", displayWidth*0.4, displayHeight*0.05);
text("s:set start point \n r: reset zoom", displayWidth*0.3, displayHeight*0.05);
text(" delete: clear \n Backspace: undo ", displayWidth*0.5, displayHeight*0.05);
text(con_status+" "+"fwd="+fwd+
" "+"Perimeter="+perimeter+" "+scale_unit+" area="+area+" Sq."+scale_unit, displayWidth*0.01, displayHeight*0.95);
///rect for buttons
noFill();
rect(displayWidth*0.28, displayHeight*0.025, displayWidth*0.35, displayHeight*0.08);
///rect for scale
noFill();
rect(displayWidth*0.64, displayHeight*0.025, displayWidth*0.12, displayHeight*0.08);
fill(255);
textSize(40);
stroke(204, 102, 0);
//text("AREA METER",displayWidth*0.05/scale_x,displayHeight*0.05/scale_y);
text("StokeMapak",displayWidth*0.05,displayHeight*0.075);
textSize(20);
stroke(204, 102, 0);
//text("AREA METER",displayWidth*0.05/scale_x,displayHeight*0.05/scale_y);
text("Developed by Vivekanand Dhakane", displayWidth*0.7, displayHeight*0.95);
}
void cord()
{
float x= (float)mouseX;
float y= (float)mouseY;
textSize(10);
fill(255);
text(x+","+y, mouseX+10, mouseY+10);
/*text(",", mouseX+30, mouseY+10);
text(y, mouseX+40, mouseY+10);*/
}
void keyPressed()
{
if (key == BACKSPACE && angles.size()>0)
{
angles.remove(angles.size()-1);
}
if (key == '+')
{
scale_x += 0.1;
scale_y += 0.1;
}
if (key == '-')
{
scale_x -= 0.1;
scale_y -= 0.1;
}
if (key == 'r')
{
scale_x = 1;
scale_y = 1;
}
if (key == 's')
{
set_first = true;
}
if (keyCode == RIGHT)
{
x_traslate += 5;
}
if (keyCode== DOWN)
{
y_traslate += 5;
}
if (keyCode == LEFT)
{
x_traslate -= 5;
}
if (keyCode== UP)
{
y_traslate -= 5;
}
if (keyCode== DELETE)
{
area=0;
if (angles.size()>0)
{
angle_Z_degree_offset=0;
angles.clear();
}
//println("Cleared");
}
}
void mousePressed()
{
if ((mouseButton == LEFT))
{
if (set_first)
{
x_start =(int)(mouseX/scale_x);
y_start =(int)(mouseY/scale_x);
set_first =false;
display_start =true;
}
}
}
void receive( byte[] data, String ip, int port ) { // <-- extended handler
// get the "real" message =
// forget the ";\n" at the end <-- !!! only for a communication with Pd !!!
// data = subset(data, 0, data.length);
String message = new String( data );
//println( "receive: \""+message+"\" from "+ip+" on port "+port );
// println("message.length()="+message.length());
if(message.equals("UDP"))
{
con_status = "Connected";
}
else if(message.equals("MIN"))
{
con_status = "Started";
}
else if(message.equals("MER"))
{
con_status = "ERROR";
}
/*if (message.length()==14)
{
int index3 =message.indexOf("@");
int index4 =message.indexOf("*");
String az_ = message.substring(index3+1, index4);
// print the result
println(az_);
return;
}*/
if (message.length()==11)
{
con_status = "Started";
data_count++;
int index2 =message.indexOf("*");
String az = message.substring(index2+1, message.length());
// print he result
//println(az);
float angle_Z_degree = Float.parseFloat(az);
if(message.charAt(0) == '0')
{
fwd=false;
angle_Z_degree += 180;
angle_Z_degree += angle_Z_degree_offset;
// angle_Z_degree_offset +=0.01;
}
else
{
fwd=true;
angle_Z_degree -= angle_Z_degree_offset;
// angle_Z_degree_offset +=0.065;
//angle_Z_degree_offset +=0.065;
}
println("area="+area+" Sq."+scale_unit);
float angle_Z_rad = angle_Z_degree *PI/180 ;//-Z_offset;
// println("angle_Z"+angle_Z);
angles.append( angle_Z_rad);
perimeter= angles.size()*unit_step_actual*actual_scale;
println("perimeter="+perimeter+" "+scale_unit);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment