Post Reply 
Approximation of Sine function in Integer Forth
03-23-2022, 08:07 AM
Post: #14
RE: Approximation of Sine function in Integer Forth
Some time ago I had to draw a simple ascii-circle for some kind of "clock project" for an Arduino. The primary goal (on Arduinos) is to write codes with small footprint and sufficient precision. I used some kind of lookup table with 16 bytes only to calculate rough values for sine (and cosine).

Maybe this code snippet can give you another idea?
You can save the division (/ 200) by expanding (and/or recalculating) the byte values to int values.

Code:

  const byte sine[] = {0, 21, 42, 62, 81, 100, 118, 134, 149, 162, 173, 183, 190, 196, 199, 200};

  static void printcircle(byte X, byte Y, byte r) { // Print circle at (X|Y) with radius r
  for (byte i = 0; i < 16; i++) {
    byte x = r * sine[15 - i] / 200, y = r * sine[i] / 200;
    byte a = X + x, b = X - x, c = Y + y, d = Y - y;
    printpixel(a, c); printpixel(a, d); printpixel(b, d); printpixel(b, c);
  }

Regards
deetee
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: Approximation of Sine function in Integer Forth - deetee - 03-23-2022 08:07 AM



User(s) browsing this thread: 4 Guest(s)