How to divide 48 equal parts on a circle ?

Hi all.

The code:

 // Draw 12 lines
  for(int i = 0; i<360; i+= 30) {
    sx = cos((i-90)*0.0174532925);
    sy = sin((i-90)*0.0174532925);
    x0 = sx*114+120;
    yy0 = sy*114+120;
    x1 = sx*100+120;
    yy1 = sy*100+120;

    tft.drawLine(x0, yy0, x1, yy1, TFT_GREEN);
  }

draw 12 lines on circle of clock.
how to get 48 equal parts lines on a circle? the interval will be 7.5 degree.
Thanks
Adam

/*
 An example analogue clock using a TFT LCD screen to show the time
 use of some of the drawing commands with the library.

 For a more accurate clock, it would be better to use the RTClib library.
 But this is just a demo. 
 
 This sketch uses font 4 only.

 Make sure all the display driver and pin connections are correct by
 editing the User_Setup.h file in the TFT_eSPI library folder.

 #########################################################################
 ###### DON'T FORGET TO UPDATE THE User_Setup.h FILE IN THE LIBRARY ######
 #########################################################################
 
 Based on a sketch by Gilchrist 6/2/2014 1.0
 */

#include <SPI.h>
#include <TFT_eSPI.h> // Hardware-specific library

#define TFT_GREY 0x5AEB

TFT_eSPI tft = TFT_eSPI();       // Invoke custom library

float sx = 0, sy = 1, mx = 1, my = 0, hx = -1, hy = 0;    // Saved H, M, S x & y multipliers
float sdeg=0, mdeg=0, hdeg=0;
uint16_t osx=120, osy=120, omx=120, omy=120, ohx=120, ohy=120;  // Saved H, M, S x & y coords
uint16_t x0=0, x1=0, yy0=0, yy1=0;
uint32_t targetTime = 0;                    // for next 1 second timeout

static uint8_t conv2d(const char* p); // Forward declaration needed for IDE 1.6.x
uint8_t hh=conv2d(__TIME__), mm=conv2d(__TIME__+3), ss=conv2d(__TIME__+6);  // Get H, M, S from compile time

bool initial = 1;

void setup(void) {
  tft.init();
  tft.setRotation(0);
  
  //tft.fillScreen(TFT_BLACK);
  //tft.fillScreen(TFT_RED);
  //tft.fillScreen(TFT_GREEN);
  //tft.fillScreen(TFT_BLUE);
  //tft.fillScreen(TFT_BLACK);
  tft.fillScreen(TFT_GREY);
  
  tft.setTextColor(TFT_WHITE, TFT_GREY);  // Adding a background colour erases previous text automatically
  
  // Draw clock face
  tft.fillCircle(120, 120, 118, TFT_GREEN);
  tft.fillCircle(120, 120, 110, TFT_BLACK);

  // Draw 12 lines
  for(int i = 0; i<360; i+= 30) {
    sx = cos((i-90)*0.0174532925);
    sy = sin((i-90)*0.0174532925);
    x0 = sx*114+120;
    yy0 = sy*114+120;
    x1 = sx*100+120;
    yy1 = sy*100+120;

    tft.drawLine(x0, yy0, x1, yy1, TFT_GREEN);
  }

  // Draw 60 dots
  for(int i = 0; i<360; i+= 6) {
    sx = cos((i-90)*0.0174532925);
    sy = sin((i-90)*0.0174532925);
    x0 = sx*102+120;
    yy0 = sy*102+120;
    // Draw minute markers
    tft.drawPixel(x0, yy0, TFT_WHITE);
    
    // Draw main quadrant dots
    if(i==0 || i==180) tft.fillCircle(x0, yy0, 2, TFT_WHITE);
    if(i==90 || i==270) tft.fillCircle(x0, yy0, 2, TFT_WHITE);
  }

  tft.fillCircle(120, 121, 3, TFT_WHITE);

  // Draw text at position 120,260 using fonts 4
  // Only font numbers 2,4,6,7 are valid. Font 6 only contains characters [space] 0 1 2 3 4 5 6 7 8 9 : . - a p m
  // Font 7 is a 7 segment font and only contains characters [space] 0 1 2 3 4 5 6 7 8 9 : .
  tft.drawCentreString("Time flies",120,260,4);

  targetTime = millis() + 1000; 
}

void loop() {
  if (targetTime < millis()) {
    targetTime += 1000;
    ss++;              // Advance second
    if (ss==60) {
      ss=0;
      mm++;            // Advance minute
      if(mm>59) {
        mm=0;
        hh++;          // Advance hour
        if (hh>23) {
          hh=0;
        }
      }
    }

    // Pre-compute hand degrees, x & y coords for a fast screen update
    sdeg = ss*6;                  // 0-59 -> 0-354
    mdeg = mm*6+sdeg*0.01666667;  // 0-59 -> 0-360 - includes seconds
    hdeg = hh*30+mdeg*0.0833333;  // 0-11 -> 0-360 - includes minutes and seconds
    hx = cos((hdeg-90)*0.0174532925);    
    hy = sin((hdeg-90)*0.0174532925);
    mx = cos((mdeg-90)*0.0174532925);    
    my = sin((mdeg-90)*0.0174532925);
    sx = cos((sdeg-90)*0.0174532925);    
    sy = sin((sdeg-90)*0.0174532925);

    if (ss==0 || initial) {
      initial = 0;
      // Erase hour and minute hand positions every minute
      tft.drawLine(ohx, ohy, 120, 121, TFT_BLACK);
      ohx = hx*62+121;    
      ohy = hy*62+121;
      tft.drawLine(omx, omy, 120, 121, TFT_BLACK);
      omx = mx*84+120;    
      omy = my*84+121;
    }

      // Redraw new hand positions, hour and minute hands not erased here to avoid flicker
      tft.drawLine(osx, osy, 120, 121, TFT_BLACK);
      osx = sx*90+121;    
      osy = sy*90+121;
      tft.drawLine(osx, osy, 120, 121, TFT_RED);
      tft.drawLine(ohx, ohy, 120, 121, TFT_WHITE);
      tft.drawLine(omx, omy, 120, 121, TFT_WHITE);
      tft.drawLine(osx, osy, 120, 121, TFT_RED);

    tft.fillCircle(120, 121, 3, TFT_RED);
  }
}

static uint8_t conv2d(const char* p) {
  uint8_t v = 0;
  if ('0' <= *p && *p <= '9')
    v = *p - '0';
  return 10 * v + *++p - '0';
}

Hello shanren

Keep in mind the trigonometry is using radians.

Take a view here to gain the knowledge:

Have a nice day and enjoy coding in C++.

2 Likes

Great!
Thank you.

Use polar coordinates
If r is the radius you want from the center of the clock and a the CCW angle with horizontal axis going through the center of the clock (so from 0 to 352.5 in 7.5° increments but in radians), then you can use
X = r cos(a)
Y = r sin(a)

1 Like

Thank you.

If you stick with radians and ignore degrees entirely then 48 equal parts are each equal to pi/24 radians. A full circle is 2*pi radians.

Something like this completely untested example.

1 Like
#define N   48
#define Rad 20
#define X0  30
#define Y0  30

char t [20];
char u [20];
char v [20];
char w [80];

// -----------------------------------------------------------------------------
void setup ()
{
    Serial.begin (9600);

    for (float ang = 0; ang < 360; ang += 360.0 / N) {
        float x = X0 + Rad * cos(ang * PI / 180);
        float y = Y0 + Rad * sin(ang * PI / 180);

        dtostrf (ang, 6, 2, t);
        dtostrf (x, 6, 2, u);
        dtostrf (y, 6, 2, v);

        sprintf (w, " %s %s %s", u, v, t);
        Serial.println (w);
    }
}

void loop ()
{
}
  50.00  30.00   0.00
  49.83  32.61   7.50
  49.32  35.18  15.00
  48.48  37.65  22.50
  47.32  40.00  30.00
  45.87  42.18  37.50
  44.14  44.14  45.00
  42.18  45.87  52.50
  40.00  47.32  60.00
  37.65  48.48  67.50
  35.18  49.32  75.00
  32.61  49.83  82.50
  30.00  50.00  90.00
  27.39  49.83  97.50
  24.82  49.32 105.00
  22.35  48.48 112.50
  20.00  47.32 120.00
  17.82  45.87 127.50
  15.86  44.14 135.00
  14.13  42.18 142.50
  12.68  40.00 150.00
  11.52  37.65 157.50
  10.68  35.18 165.00
  10.17  32.61 172.50
  10.00  30.00 180.00
  10.17  27.39 187.50
  10.68  24.82 195.00
  11.52  22.35 202.50
  12.68  20.00 210.00
  14.13  17.82 217.50
  15.86  15.86 225.00
  17.82  14.13 232.50
  20.00  12.68 240.00
  22.35  11.52 247.50
  24.82  10.68 255.00
  27.39  10.17 262.50
  30.00  10.00 270.00
  32.61  10.17 277.50
  35.18  10.68 285.00
  37.65  11.52 292.50
  40.00  12.68 300.00
  42.18  14.13 307.50
  44.14  15.86 315.00
  45.87  17.82 322.50
  47.32  20.00 330.00
  48.48  22.35 337.50
  49.32  24.82 345.00
  49.83  27.39 352.50
1 Like

Thanks for all.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.