goodmanrs / u8glib

Automatically exported from code.google.com/p/u8glib
Other
0 stars 0 forks source link

Missing Draw orientd line (Radius) #157

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Actually it is not an issue, but I didn.t know where to post.

I needed to draw a line (radius), from an origin oriented for acertain number 
of degree. I wirted the following code, and I would like to share.

#include "U8glib.h"

#define ToRad(x) (x*0.01745329252)  // *pi/180
#define ToDeg(x) (x*57.2957795131)  // *180/pi

// setup u8g object, please remove comment from one of the following 
constructor calls
// IMPORTANT NOTE: The complete list of supported devices is here: 
http://code.google.com/p/u8glib/wiki/device
int bank = 0;
float bank_rad=0;
int line_x1 = 0;
int line_y1 = 0;
int line_x2 = 0;
int line_y2 = 0;

U8GLIB_ST7920_128X64_1X u8g(8, 9, 10, 11, 4, 5, 6, 7, 18, 17, 16);   // 8Bit 
Com: D0..D7: 8,9,10,11,4,5,6,7 en=18, di=17,rw=16

void draw(void) {
  // graphic commands to redraw the complete screen should be placed here  
  u8g.setFont(u8g_font_unifont);
  //u8g.setFont(u8g_font_osb21);
  //u8g.drawStr( 0, 22, "stringa");
  u8g.setPrintPos(75,32);
  u8g.print(bank);
  u8g.drawCircle (32,32,31);
  bank_rad = ToRad(bank);
  line_x1 = (float)32+(int(-32*cos(bank_rad)));
  line_y1 = (float)32+(int(-32*sin(bank_rad)));
  line_x2 = (float)32+(int(32*cos(bank_rad)));
  line_y2 = (float)32+(int(32*sin(bank_rad)));
  u8g.drawLine(line_x1,line_y1,line_x2,line_y2);
}

void drawOrientedLine(int centro_x, int centro_y, int angolo, int max_x , int 
max_y) {
// This function draws a line from an origin point, the orientation of the line 
is passed as degrees. The arguments max_x and max_y indicate the limit for the 
line drawing.
// Orientation in degrees is from the three O'clock position. 0 deg. = 3 
O'clock ; 90 = 6 O'clock ; 180 deg = 9 O'clock ; 270 deg = 12 O'clock
int x = centro_x;
int y = centro_y;
float coord_float =0;
float radius = 0;
int tronc =0;
float resto=0;
// 316 - 360 deg ; 0 - 45 deg
if ((angolo >=0 && angolo <=45)||(angolo>=316 && angolo<=360))
{  do {
     u8g.drawPixel (x, y);
     x++;
     radius = (float)(x-centro_x) / cos(ToRad(angolo));
     coord_float = (float)centro_y+(radius*sin(ToRad(angolo)));
     tronc = floor(coord_float);
     resto=(float)coord_float - tronc;
     if (resto>= 0.49) y=ceil(coord_float); 
     else y=floor(coord_float); 
    }while (x<=max_x && y <=max_y && x>=0 && y>= 0) ;
}
// 46-90 /  91-135
if ((angolo >=46 && angolo <=135))
{  do {
     u8g.drawPixel (x, y);
     y++;
     radius = (float)(y-centro_y) / sin(ToRad(angolo));
     coord_float = (float)centro_x+(radius*cos(ToRad(angolo)));
     tronc = floor(coord_float);
     resto=(float)coord_float - tronc;
     if (resto>= 0.49) x=ceil(coord_float); 
     else x=floor(coord_float); 
    }while (x<=max_x && y <=max_y && x>=0 && y>= 0) ;
}

 //  136-180 /  181 - 225 
if ((angolo>=136 && angolo<=225))
{  do {
     u8g.drawPixel (x, y);
     x--;
     radius = (float)(centro_x-x) / cos(ToRad(angolo));
     coord_float = (float)centro_y-(radius*sin(ToRad(angolo)));
     tronc = floor(coord_float);
     resto=(float)coord_float - tronc;
     if (resto>= 0.49) y=ceil(coord_float); 
     else y=floor(coord_float); 
    }while (x<=max_x && y <=max_y && x>=0 && y>= 0) ;
} 

// 226-270 /  271-315
if ((angolo >=226 && angolo <=315))
{  do {
     u8g.drawPixel (x, y);
     y--;
     radius = (float)(centro_y-y) / sin(ToRad(angolo));
     coord_float = (float)centro_x-(radius*cos(ToRad(angolo)));
     tronc = floor(coord_float);
     resto=(float)coord_float - tronc;
     if (resto>= 0.49) x=ceil(coord_float); 
     else x=floor(coord_float); 
    }while (x<=max_x && y <=max_y && x>=0 && y>= 0) ;
}

}

void setup(void) {
  Serial.begin(9600);
  // flip screen, if required
   u8g.setRot180();

  // set SPI backup if required
  //u8g.setHardwareBackup(u8g_backup_avr_spi);

  // assign default color value
  if ( u8g.getMode() == U8G_MODE_R3G3B2 ) 
    u8g.setColorIndex(255);     // white
  else if ( u8g.getMode() == U8G_MODE_GRAY2BIT )
    u8g.setColorIndex(3);         // max intensity
  else if ( u8g.getMode() == U8G_MODE_BW )
    u8g.setColorIndex(1);         // pixel on
}

void loop(void) {
  for (int i = 0; i<=360 ; i++){

 u8g.firstPage();  
  do {
  drawOrientedLine(64, 32, i, 128 , 64);
 // if (i>=180)  drawOrientedLine(64, 32, i-180, 128 , 64); 
//  else drawOrientedLine(64, 32, i+180, 128 , 64);
    } while( u8g.nextPage() );
//delay (1);
  }

}

Original issue reported on code.google.com by marco.di...@gmail.com on 15 Apr 2013 at 5:43

Attachments:

GoogleCodeExporter commented 8 years ago
I think posting such a nice example in the Exhibition / Gallery of the Arduino 
forum or adding another comment to the u8glib thread 
(http://arduino.cc/forum/index.php/topic,91395.0.html) might reach more people. 
In both cases, a picture would be great.

Original comment by olikr...@gmail.com on 15 Apr 2013 at 7:00

GoogleCodeExporter commented 8 years ago
closing this issue

Original comment by olikr...@gmail.com on 4 Jun 2013 at 8:13