Loading...
Logo
Processing Forum

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.
 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(); }
  
}
//-------------------------------------------
//-------------------------------------------



Replies(3)

which line number do you mean?


Copy code
  1. void setup(){
  2.   background(#ffffff);
  3.   size(X, Y); 
  4.  pos1 = new PVector(200,200);
  5. pos2=new PVector(210,210); 
  6. }
  7. //-------------------------------------------
  8. //-------------------------------------------
  9. PVector pos1,pos2;
  10. int X=400;//SZEROKOSC OKNA
  11. int Y=400;//WYSOKOSC OKNA
  12. int Error=0;//ZMIENNA GLOBALNA ODPOWIADAJACE ZA PRZERWANIE PETLI
  13. float CZUJNIK=0;//Z GLOBALNA ODPOWIADAJACE ZA WIDZENIE
  14. int x=0;//Z GLOBALNA DO RYSOWANIA FUNKCJI
  15. int y=0;//Z GLOBALNA DO RYSOWANIA FUNKCJI
  16. int k=0;//Z ODPOWIADAJACA ZA ILOSC WYSWIETLONYCH KLATEK
  17. //color black=#000000;//KOLOR CZARNY POTRZEBNY DO WYKRYWANIA SCIAN
  18. //-------------------------------------------
  19. //-------------------------------------------
  20.  
  21. void draw(){
  22. background(255);//ZAWSZE RYSOWANE PODLOZE
  23. pos1.x=200;
  24. pos1.y=200;
  25. point(pos1.x,pos1.y);
  26. //point(x2,y2);
  27. pos2.x=mouseX;//X PUNKTU 2 PODOZA ZA CURSOREM
  28. pos2.y=mouseY;//Y PUNKTU 2 PODOZA ZA CURSOREM
  29. //x2=20; //X PUNKTU 2 JEST STALE
  30. //y2=250; //Y PUNKTU 2 JEST STALE
  31. //RAMKA GORNA1
  32. stroke(#F2EE64);
  33. fill(#F2EE64);
  34. rect(0,0,180,40);
  35. fill(#ff0000);
  36. text("x1= " + pos1.x, 65, 15);
  37. text("y1= " + pos1.y, 65, 30);
  38. text("x2= " + pos2.x, 125, 15);
  39. text("y2= " + pos2.y, 125, 30);
  40. //------------------------
  41. //CZARNY RECT
  42. stroke(#000000);
  43. fill(0);
  44. rect(370,80,20,180);
  45. rect(180,0,190,40);
  46. //------------------------
  47. //RAMKA GORNA1
  48. stroke(#F2EE64);
  49. fill(#F2EE64);
  50. //rect(180,0,190,40);
  51. fill(#ff0000);
  52. text("CZUJNIK= " + CZUJNIK, 180,20);
  53. //------------------------
  54. CZUJNIK=(rysuj2(pos1,pos2,X,Y));
  55. k=k+1;
  56. println(k);
  57. //if(k==1) //ZABEZPIECZENIA JEŚLI SPRAWDZAM 1 KLATKE
  58. //stop();
  59. }
  60. //KONIEC
  61. //-------------------------------------------
  62. //-------------------------------------------
  63. //FUNKCJE
  64. float zao(float x)
  65. {
  66.   x=x*10;
  67.   x=round(x);
  68.   x=x/10;
  69.   return(x);
  70. }
  71. //-------------------------------------------
  72. //-------------------------------------------
  73. float rysuj2(PVector pos1,PVector pos2,int X,int Y)
  74. {
  75. //a i b to współczynniki
  76.   float a=0;
  77.   float b=0;
  78.   color black=#000000;//KOLOR CZARNY
  79.   color cp;//BADANY KOLOR
  80.   float odl=0;
  81. //------------------------
  82. //OBLICZENIA FUNKCJI
  83. a=(pos2.y-pos1.y);
  84. a=a/(pos2.x-pos1.x);
  85. b=pos1.y-a*pos1.x;
  86. //------------------------
  87.   
  88. //------------------------
  89.   stroke(#FF0000);
  90.  if(pos1.x==pos2.x)
  91.         {
  92.           if(pos1.y>pos2.y)
  93.    for(y=int(pos1.y);Error!=1;y++)
  94.  {
  95.   if(y==Y)
  96.  {  Error=1;}
  97.  if(y==0)
  98.  {  Error=1;}
  99.  fill(#ff0000);
  100.  stroke(#ff0000);
  101.  //println(pos2.x+" "+y);
  102.  cp=get(int(pos1.x),y);
  103.  if(cp==black)
  104. {Error=1;
  105. odl=(pos1.y-y);}
  106. point(pos2.x,y);
  107.  }  
  108.      else
  109.    for(y=int(pos1.y);Error!=1;y--)
  110.  {
  111.   if(y==Y)
  112.  {  Error=1;}
  113.  if(y==0)
  114.  {  Error=1;}
  115.  fill(#ff0000);
  116.  stroke(#ff0000);
  117. // println(pos2.x+" "+y);
  118.  cp=get(int(pos1.x),y);
  119. if(cp==black)
  120. {Error=1;
  121. odl=(pos1.y-y);}
  122. point(pos2.x,y);
  123.  }     
  124. Error=0;
  125. return(odl); 
  126.         }
  127. //-------------------------------------------
  128. //-------------------------------------------        
  129.   if(pos1.x>pos2.x)
  130.  for(x=int(pos1.x);Error!=1;x++)
  131.  {
  132.    y=int(a*x+b);
  133.   
  134.  if(x==X)
  135.  { Error=1;}
  136.  if(x==0)
  137.  { Error=1;}
  138.   if(y==Y)
  139.  {  Error=1;}
  140.  if(y==0)
  141.  { Error=1;}
  142. //------------------------
  143. cp=get(x,y);
  144. if(cp==black)
  145. {Error=1;
  146. odl=(pos1.x-x)*(pos1.x-x)+(pos1.y-y)*(pos1.y-y);
  147. odl=sqrt(odl);}
  148.  point(x,y);
  149.  }
  150.  else
  151.  for(x=int(pos1.x);Error!=1;x--)
  152.  {
  153.    y=int(a*x+b);
  154.   
  155.  if(x==X)
  156.  { Error=1;}
  157.  if(x==0)
  158.  { Error=1;}
  159.   if(y==Y)
  160.  { Error=1;}
  161.  if(y==0)
  162.  { Error=1;}
  163.  //------------------------
  164. cp=get(x,y);
  165. if(cp==black)
  166. {Error=1;
  167. odl=(pos1.x-x)*(pos1.x-x)+(pos1.y-y)*(pos1.y-y);
  168. odl=sqrt(odl);}
  169. point(x,y); 
  170.  }
  171. Error=0;
  172. x=0;
  173. y=0;
  174. return(odl);
  175. }
  176. //-------------------------------------------
  177. //-------------------------------------------
  178. void keyPressed()
  179. {    
  180.   if(key=='q')
  181.   {exit(); }
  182.   
  183. }
  184. //-------------------------------------------//------------------------------------------

Good you made some comments. Too bad I don't read Polish (if that's this language. I am not a specialist of East Europe languages). Note that good indentation is important too for easy reading of code.
Fortunately, we have Ctrl+T in the PDE to re-format your code.

Some generic notes:
- X and Y aren't very good names for the dimension of the sketch.
- In Processing, it is better to put numerical values in size() (because they are used when exporting the sketch), and to use the width and height constants instead. (I made the same error when I started...)
- The small red text on black background is hard to read, at least for me.
- For variables like Error taking only two values (0 and 1) used only in tests, it is better to use a boolean variable instead of int. Thus you can set it to true or false and you can test it like for (y = int(pos1.y); Error; y--)

When a becomes big (in absolute value), you are starting to have problems. That's because for such values, the step between two points becomes bigger than the sketch's dimensions!
The cure is to increment on y not only on a specific value (when a is infinite) but much earlier. The positive side effect is that the ray is less diluted in these cases.
I also used a trick to reduce code duplication, doing the increment / decrement of the relevant variable depending on a boolean variable computed upfront.
Code can be simplified further, but I kept changes minimal so you can recognize your code...

Copy code
  1. void setup() {
  2.   size(400, 400);
  3.   background(#ffffff);
  4.   pos1 = new PVector(200, 200);
  5.   pos2=new PVector(210, 210);
  6. }
  7. //-------------------------------------------
  8. //-------------------------------------------
  9. PVector pos1, pos2;
  10. int Error=0;//ZMIENNA GLOBALNA ODPOWIADAJACE ZA PRZERWANIE PETLI
  11. float CZUJNIK=0;//Z GLOBALNA ODPOWIADAJACE ZA WIDZENIE
  12. int x=0;//Z GLOBALNA DO RYSOWANIA FUNKCJI
  13. int y=0;//Z GLOBALNA DO RYSOWANIA FUNKCJI
  14. int k=0;//Z ODPOWIADAJACA ZA ILOSC WYSWIETLONYCH KLATEK
  15. //color black=#000000;//KOLOR CZARNY POTRZEBNY DO WYKRYWANIA SCIAN
  16. //-------------------------------------------
  17. //-------------------------------------------
  18. void draw() {
  19.   background(255);//ZAWSZE RYSOWANE PODLOZE
  20.   pos1.x=200;
  21.   pos1.y=200;
  22.   point(pos1.x, pos1.y);
  23.   //point(x2,y2);
  24.   pos2.x=mouseX;//X PUNKTU 2 PODOZA ZA CURSOREM
  25.   pos2.y=mouseY;//Y PUNKTU 2 PODOZA ZA CURSOREM
  26.   //x2=20; //X PUNKTU 2 JEST STALE
  27.   //y2=250; //Y PUNKTU 2 JEST STALE
  28.   //RAMKA GORNA1
  29.   stroke(#F2EE64);
  30.   fill(#F2EE64);
  31.   rect(0, 0, 180, 40);
  32.   fill(#ff0000);
  33.   text("x1= " + pos1.x, 65, 15);
  34.   text("y1= " + pos1.y, 65, 30);
  35.   text("x2= " + pos2.x, 125, 15);
  36.   text("y2= " + pos2.y, 125, 30);
  37.   //------------------------
  38.   //CZARNY RECT
  39.   stroke(#000000);
  40.   fill(228);
  41.   rect(370, 80, 20, 180);
  42.   rect(180, 0, 190, 40);
  43.   //------------------------
  44.   //RAMKA GORNA1
  45.   stroke(#F2EE64);
  46.   fill(#F2EE64);
  47.   //rect(180,0,190,40);
  48.   fill(#ff0000);
  49.   text("CZUJNIK= " + CZUJNIK, 180, 20);
  50.   //------------------------
  51.   CZUJNIK=(rysuj2(pos1, pos2));
  52.   k=k+1;
  53.   //println(k);
  54.   //if(k==1) //ZABEZPIECZENIA JESLI SPRAWDZAM 1 KLATKE
  55.   //stop();
  56. }
  57. //KONIEC
  58. //-------------------------------------------
  59. //-------------------------------------------
  60. //FUNKCJE
  61. float zao(float x)
  62. {
  63.   x=x*10;
  64.   x=round(x);
  65.   x=x/10;
  66.   return x;
  67. }
  68. //-------------------------------------------
  69. //-------------------------------------------
  70. float rysuj2(PVector pos1, PVector pos2)
  71. {
  72.   //a i b to wspólczynniki
  73.   float a=0;
  74.   float b=0;
  75.   color black=#000000;//KOLOR CZARNY
  76.   color cp;//BADANY KOLOR
  77.   float odl=0;
  78.   //------------------------
  79.   //OBLICZENIA FUNKCJI
  80.   a=(pos2.y-pos1.y);
  81.   a=a/(pos2.x-pos1.x);
  82.   b=pos1.y-a*pos1.x;
  83.   println(a + " " + b);
  84.   //------------------------
  85.   //------------------------
  86.   stroke(#FF0000);
  87.   if (abs(a) > 2) // That's the criterion to change increment kind
  88.   {
  89.     boolean incr = pos1.y > pos2.y;
  90.     y = int(pos1.y);
  91.     while (Error != 1)
  92.     {
  93.       x = int((y - b) / a);
  94.      
  95.       if (y==height)
  96.       { 
  97.         Error=1;
  98.       }
  99.       if (y==0)
  100.       { 
  101.         Error=1;
  102.       }
  103.       stroke(#ff0000);
  104.       // println(pos2.x+" "+y);
  105.       cp=get(x, y);
  106.       if (cp==black)
  107.       {
  108.         Error=1;
  109.         odl=(pos1.y-y);
  110.       }
  111.       point(x, y);
  112.      
  113.       if (incr) y++;
  114.       else y--;
  115.     }    
  116.   }
  117.   else
  118.   //-------------------------------------------
  119.   //-------------------------------------------       
  120.   {
  121.     boolean incr = pos1.x > pos2.x;
  122.     x = int(pos1.x);
  123.     while (Error != 1)
  124.     {
  125.       y = int(a * x + b);
  126.       if (x==width)
  127.       {
  128.         Error=1;
  129.       }
  130.       if (x==0)
  131.       {
  132.         Error=1;
  133.       }
  134.       if (y==Y)
  135.       { 
  136.         Error=1;
  137.       }
  138.       if (y==0)
  139.       {
  140.         Error=1;
  141.       }
  142.       //------------------------
  143.       cp=get(x, y);
  144.       if (cp==black)
  145.       {
  146.         Error=1;
  147.         odl=(pos1.x-x)*(pos1.x-x)+(pos1.y-y)*(pos1.y-y);
  148.         odl=sqrt(odl);
  149.       }
  150.       stroke(#FF00FF);
  151.       point(x, y);
  152.      
  153.       if (incr) x++;
  154.       else x--;
  155.     }
  156.   }
  157.   Error=0;
  158.   x=0;
  159.   y=0;
  160.   return odl;
  161. }
  162. //-------------------------------------------
  163. //-------------------------------------------
  164. void keyPressed()
  165. {   
  166.   if (key=='q')
  167.   {
  168.     exit();
  169.   }
  170. }
  171. //-------------------------------------------
  172. //-------------------------------------------

Thanks a lot:) Yes I am from Poland.I didn't think that I will need help . Next time I will write in English just in case.It can be stupid but I don't know that i can use simple "width" and "height".You are right red letter on black screen wasn't easily readable.
Using "while" was really good :) I do not know why i didn't think about that:)