We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi everybdy, here's my code. i Can't understand where i fail... when defining floats or in the if part? is that a script-structure error? the PShape part doesn't really matters, to ceck if it works can also be used squares... thanks!
import controlP5.*;
ControlP5 cp5;
Accordion accordion;
PShape A;
PShape B;
PShape C;
void setup() {
size(500, 700);
colorMode(HSB, 360, 100, 100);
noStroke();
A = loadShape("Aprocessing.svg");
B = loadShape("Bprocessing.svg");
C = loadShape("Cprocessing.svg");
smooth();
gui();
}
void gui() {
cp5 = new ControlP5(this);
// group
Group g = cp5.addGroup("posizione")
.setBackgroundColor(color(0, 60))
.setBackgroundHeight(200)
;
cp5.addSlider("posizioAx")
.setPosition(10,20)
.setSize(100,20)
.setRange(0,400)
.setValue(200)
.moveTo(g)
;
cp5.addSlider("posizioAy")
.setPosition(10,50)
.setSize(100,20)
.setRange(0,400)
.setValue(200)
.moveTo(g)
;
cp5.addSlider("posizioBx")
.setPosition(10,80)
.setSize(100,20)
.setRange(0,600)
.setValue(100)
.moveTo(g)
;
cp5.addSlider("posizioBy")
.setPosition(10,110)
.setSize(100,20)
.setRange(0,600)
.setValue(100)
.moveTo(g)
;
cp5.addSlider("posizioCx")
.setPosition(10,140)
.setSize(100,20)
.setRange(0,600)
.setValue(100)
.moveTo(g)
;
cp5.addSlider("posizioCy")
.setPosition(10,170)
.setSize(100,20)
.setRange(0,600)
.setValue(100)
.moveTo(g)
;
// create a new accordion
// add g the accordion.
accordion = cp5.addAccordion("acc")
.setPosition(0,0)
.setWidth(200)
.addItem(g)
;
cp5.mapKeyFor(new ControlKey() {public void keyEvent() {accordion.open(0,1,2);}}, 'o');
cp5.mapKeyFor(new ControlKey() {public void keyEvent() {accordion.close(0,1,2);}}, 'c');
cp5.mapKeyFor(new ControlKey() {public void keyEvent() {accordion.setWidth(300);}}, '1');
cp5.mapKeyFor(new ControlKey() {public void keyEvent() {accordion.setPosition(0,0);accordion.setItemHeight(190);}}, '2');
cp5.mapKeyFor(new ControlKey() {public void keyEvent() {accordion.setCollapseMode(ControlP5.ALL);}}, '3');
cp5.mapKeyFor(new ControlKey() {public void keyEvent() {accordion.setCollapseMode(ControlP5.SINGLE);}}, '4');
cp5.mapKeyFor(new ControlKey() {public void keyEvent() {cp5.remove("myGroup1");}}, '0');
accordion.open(0,1,2);
accordion.setCollapseMode(Accordion.MULTI);
}
void draw() {
background(360);
//intA
float posAx = cp5.getController("posizioAx").getValue();
float posAy = cp5.getController("posizioAy").getValue();
//intB
float posBx = cp5.getController("posizioBx").getValue();
float posBy = cp5.getController("posizioBy").getValue();
//intC
float posCx = cp5.getController("posizioCx").getValue();
float posCy = cp5.getController("posizioCy").getValue();
//distanze
float dAB = dist(posAx, posAy, posBx, posBy);
float dAC = dist(posAx, posAy, posCx, posCy);
float dBC = dist(posBx, posBy, posCx, posCy);
float p = dAB + dAC + dBC ;
//per caso 1
float dA1 = (100/p)*(p-dBC) ;
float dB1 = (50/p)*(dBC+p) ;
float dC1 = (50/p)*(dBC+p) ;
//per caso 2
float dA2 = (50/p)*(dAB+p) ;
float dB2 = (50/p)*(dAB+p) ;
float dC2 = (100/p)*(p-dAB) ;
//per caso 3
float dA3 = (50/p)*(dAC+p) ;
float dB3 = (100/p)*(p-dAC) ;
float dC3 = (50/p)*(dAC+p) ;
//caso 0
if (dBC == (1/3)*p) {
// forme
A.disableStyle(); // Ignore the colors in the SVG
shape(A, posAx, posAy, (2/3)*100, (2/3)*100);
fill(50, 50, 50);
B.disableStyle(); // Ignore the colors in the SVG
shape(B, posBx, posBy, (2/3)*100, (2/3)*100);
fill(50, 50, 50);
C.disableStyle(); // Ignore the colors in the SVG
shape(C, posCx, posCy, (2/3)*100, (2/3)*100);
fill(50, 50, 50);
}
//caso 1
if (dBC < (1/3)*p) {
// forme
A.disableStyle(); // Ignore the colors in the SVG
shape(A, posAx, posAy, dA1, dA1);
fill(50, 50, 50);
B.disableStyle(); // Ignore the colors in the SVG
shape(B, posBx, posBy, dB1, dB1);
fill(50, 50, 50);
C.disableStyle(); // Ignore the colors in the SVG
shape(C, posCx, posCy, dC1, dC1);
fill(50, 50, 50);
}
//caso2
if (dAB < (1/3)*p) {
// forme
A.disableStyle(); // Ignore the colors in the SVG
shape(A, posAx, posAy, dA2, dA2);
fill(50, 50, 50);
B.disableStyle(); // Ignore the colors in the SVG
shape(B, posBx, posBy, dB2, dB2);
fill(50, 50, 50);
C.disableStyle(); // Ignore the colors in the SVG
shape(C, posCx, posCy, dC2, dC2);
fill(50, 50, 50);
}
// caso 3
if (dAC < (1/3)*p) {
// forme
A.disableStyle(); // Ignore the colors in the SVG
shape(A, posAx, posAy, dA3, dA3);
fill(50, 50, 50);
B.disableStyle(); // Ignore the colors in the SVG
shape(B, posBx, posBy, dB3, dB3);
fill(50, 50, 50);
C.disableStyle(); // Ignore the colors in the SVG
shape(C, posCx, posCy, dC3, dC3);
fill(50, 50, 50);
}
}
Answers
In Java (in C/C++/C#/D as well), when both operands of a division operation are whole values, its fractional part is removed! @-)
See for yourself ->
println(1/3);
To force a fractional result, we gotta have at least 1 of them to be a fractional value ->
println(1/3.0);
Another way is prefix 1 of them w/ a cast operator ->
println((float) 1/3);
A pity Processing's reference doesn't warn about such important issue. And also that in JavaScript there's no whole division:
processing.org/reference/divide.html
Thank you so much for answerin' but i don't understand it totally, i'm very new to processing and programming :(
I have to declare
println
every time? where i have to put it? can you please give me an example in a script portion? Exusme but i'm learning in a very emphirical way...Thanks again!
Function println() merely displays a String at the console.
I've posted
println(1/3);
so you can type & see for yourself the result from that is 0 instead of 0.333...!Those lines #187, #210, #231, #251 you've posted above like
if (dBC == (1/3)*p) {
, are equivalent toif (dBC == 0) {
Ok! now i understand! and so how can i solve this? how can i use this kind of numbers?
Thanks!
I've already answered that in my 1st post! Use fractional numbers or a cast operator like (float) or (double)!
Sorry i'm quite bad in english too! thanks so much!