I’ve been looking at the picture of the graph on the ad for the new Pico GFX Pack and it appears to have been drawn up-side-down. As the angle increases from zero the value of sine(a) increases. On the picture it drops.
Line 29 in the example calc.py is
y = int((sin(radians(angle + offset)) * 24) + 32)
I think it should be:
y = int(32 - (sin(radians(angle + offset)) * 24))
where 32 is the x axis pixel position
I’ve not got a GFX but the monochrome SSD1306 has the same pixel size and produces this:
# Sine graph on 128 X 64 pixel display (SSD1306)
# Tony Goodhew 19th Nov 2022
import utime, math
from machine import Pin, I2C
from ssd1306 import SSD1306_I2C
import framebuf
sda=machine.Pin(0) # 0
scl=machine.Pin(1) # 1
i2c=machine.I2C(0,sda=sda, scl=scl, freq=400000)
print(i2c.scan()) # SSD1306 I2C address
oled = SSD1306_I2C(128, 64, i2c)
def blk():
oled.fill(0)
oled.show()
blk() # Clear the oled display
for x in range(121):
oled.pixel(x,32,1)
a = -360
for i in range(121):
yy = int(math.sin(math.radians(a))* -20)
oled.pixel(i,32+yy,1)
a = a + 6
for yy in range(10,54): # y-axis
oled.pixel(60,yy,1)
for yy in range(12,54,10): # Ticks
for xx in range(60,64):
oled.pixel(xx,yy,1)
oled.text("y = sin(x)",2,1) # Titles
oled.text("x = -360:360",2,56)
oled.show()
Wow, good spot! That’s what I call attention to detail and with a completly corrected version too.
I hope nobody failed their maths GCSE because of that mistake! (unlikely)
Pimoroni: For a minimalist correction on the original, just change the formula on the screen to y=-sin(x)
In the spirit of the original point from @Tonygo2
You changed the graphic to be y=sin(-x) which I’ll let you ‘get away with’. I’m no mathematician, but even though,
sin(-x) == -sin(x)
I think it comes more unstuck when you realise that
cos(-x) != -cos(x)
The problem is that the screen co-ordinate system and ‘normal’ graphing of y values are in opposite directions. The screen has the origin at top left and positive y is DOWNWARDS.
To draw the cosine curve:
# Cosine graph on 128 X 64 pixel display (SSD1306)
# Tony Goodhew 23rd Nov 2022
import utime, math
from machine import Pin, I2C
from ssd1306 import SSD1306_I2C
import framebuf
sda=machine.Pin(0) # 0
scl=machine.Pin(1) # 1
i2c=machine.I2C(0,sda=sda, scl=scl, freq=400000)
print(i2c.scan()) # SSD1306 I2C address
oled = SSD1306_I2C(128, 64, i2c)
def blk():
oled.fill(0)
oled.show()
blk() # Clear the oled display
for x in range(121):
oled.pixel(x,32,1)
a = -360
for i in range(121):
yy = int(math.cos(math.radians(a))* -20)
oled.pixel(i,32+yy,1)
a = a + 6
for yy in range(10,54): # y-axis
oled.pixel(60,yy,1)
for yy in range(12,54,10): # Ticks
for xx in range(60,64):
oled.pixel(xx,yy,1)
oled.text("y = cos(x)",2,1) # Titles
oled.text("x = -360:360",2,56)
oled.show()
Alternative line for the main calculation is:
yy = -int(math.cos(math.radians(a))* 20)
More of an issue with coordinate geometry than trigonometry.