laser distance sensor
in
Programming Questions
•
1 year ago
Hi all.I must make some model of laser distance sensor.Idea is quite simple.There is 2 point and i must have a linear function.That function make a red pixel across the screen and stop when there is some black pixel.Then it returns me a distance.
void setup(){
I wrote function and it was quite simple but there i have problem. I wrote linear function using
y1=ax1+b
y2=ax2+b
When x1=x2 it is no function in math "a" is infiniti and it doesn't work properly so i wrote a small extra code what to do when x1=x2.In fact it work ok when x1=x2.There is one extra problem when "a" is big there is printed really small amount of pixel and distance isn't correct.I Know that it can be abstarct so i quote code below.Thanks for all reply.:)
void setup(){
background(#ffffff);
size(X, Y);
pos1 = new PVector(200,200);
pos2=new PVector(210,210);
}
//-------------------------------------------
//-------------------------------------------
PVector pos1,pos2;
int X=400;//SZEROKOSC OKNA
int Y=400;//WYSOKOSC OKNA
int Error=0;//ZMIENNA GLOBALNA ODPOWIADAJACE ZA PRZERWANIE PETLI
float CZUJNIK=0;//Z GLOBALNA ODPOWIADAJACE ZA WIDZENIE
int x=0;//Z GLOBALNA DO RYSOWANIA FUNKCJI
int y=0;//Z GLOBALNA DO RYSOWANIA FUNKCJI
int k=0;//Z ODPOWIADAJACA ZA ILOSC WYSWIETLONYCH KLATEK
//color black=#000000;//KOLOR CZARNY POTRZEBNY DO WYKRYWANIA SCIAN
//-------------------------------------------
//-------------------------------------------
void draw(){
background(255);//ZAWSZE RYSOWANE PODLOZE
pos1.x=200;
pos1.y=200;
point(pos1.x,pos1.y);
//point(x2,y2);
pos2.x=mouseX;//X PUNKTU 2 PODOZA ZA CURSOREM
pos2.y=mouseY;//Y PUNKTU 2 PODOZA ZA CURSOREM
//x2=20; //X PUNKTU 2 JEST STALE
//y2=250; //Y PUNKTU 2 JEST STALE
//RAMKA GORNA1
stroke(#F2EE64);
fill(#F2EE64);
rect(0,0,180,40);
fill(#ff0000);
text("x1= " + pos1.x, 65, 15);
text("y1= " + pos1.y, 65, 30);
text("x2= " + pos2.x, 125, 15);
text("y2= " + pos2.y, 125, 30);
//------------------------
//CZARNY RECT
stroke(#000000);
fill(0);
rect(370,80,20,180);
rect(180,0,190,40);
//------------------------
//RAMKA GORNA1
stroke(#F2EE64);
fill(#F2EE64);
//rect(180,0,190,40);
fill(#ff0000);
text("CZUJNIK= " + CZUJNIK, 180,20);
//------------------------
CZUJNIK=(rysuj2(pos1,pos2,X,Y));
k=k+1;
println(k);
//if(k==1) //ZABEZPIECZENIA JEŚLI SPRAWDZAM 1 KLATKE
//stop();
}
//KONIEC
//-------------------------------------------
//-------------------------------------------
//FUNKCJE
float zao(float x)
{
x=x*10;
x=round(x);
x=x/10;
return(x);
}
//-------------------------------------------
//-------------------------------------------
float rysuj2(PVector pos1,PVector pos2,int X,int Y)
{
//a i b to współczynniki
float a=0;
float b=0;
color black=#000000;//KOLOR CZARNY
color cp;//BADANY KOLOR
float odl=0;
//------------------------
//OBLICZENIA FUNKCJI
a=(pos2.y-pos1.y);
a=a/(pos2.x-pos1.x);
b=pos1.y-a*pos1.x;
//------------------------
//------------------------
stroke(#FF0000);
if(pos1.x==pos2.x)
{
if(pos1.y>pos2.y)
for(y=int(pos1.y);Error!=1;y++)
{
if(y==Y)
{ Error=1;}
if(y==0)
{ Error=1;}
fill(#ff0000);
stroke(#ff0000);
//println(pos2.x+" "+y);
cp=get(int(pos1.x),y);
if(cp==black)
{Error=1;
odl=(pos1.y-y);}
point(pos2.x,y);
}
else
for(y=int(pos1.y);Error!=1;y--)
{
if(y==Y)
{ Error=1;}
if(y==0)
{ Error=1;}
fill(#ff0000);
stroke(#ff0000);
// println(pos2.x+" "+y);
cp=get(int(pos1.x),y);
if(cp==black)
{Error=1;
odl=(pos1.y-y);}
point(pos2.x,y);
}
Error=0;
return(odl);
}
//-------------------------------------------
//-------------------------------------------
if(pos1.x>pos2.x)
for(x=int(pos1.x);Error!=1;x++)
{
y=int(a*x+b);
if(x==X)
{ Error=1;}
if(x==0)
{ Error=1;}
if(y==Y)
{ Error=1;}
if(y==0)
{ Error=1;}
//------------------------
cp=get(x,y);
if(cp==black)
{Error=1;
odl=(pos1.x-x)*(pos1.x-x)+(pos1.y-y)*(pos1.y-y);
odl=sqrt(odl);}
point(x,y);
}
else
for(x=int(pos1.x);Error!=1;x--)
{
y=int(a*x+b);
if(x==X)
{ Error=1;}
if(x==0)
{ Error=1;}
if(y==Y)
{ Error=1;}
if(y==0)
{ Error=1;}
//------------------------
cp=get(x,y);
if(cp==black)
{Error=1;
odl=(pos1.x-x)*(pos1.x-x)+(pos1.y-y)*(pos1.y-y);
odl=sqrt(odl);}
point(x,y);
}
Error=0;
x=0;
y=0;
return(odl);
}
//-------------------------------------------
//-------------------------------------------
void keyPressed()
{
if(key=='q')
{exit(); }
}
//-------------------------------------------
//-------------------------------------------
1