2

"EXERCISE 52:RAINBOW TRIANGLES Color each triangle of the rotating triangle sketch using stroke()."

It should look like this:

enter image description here

above is the picture my code:

def setup():
    colorMode(HSB) 
    size(600,600)
t = 0

def draw():
    global t
    background(255)#white
    translate (width/2, height/2)
    for i in range(90):
        stroke(3*i,255,255)
        rotate(radians(360/90))
        pushMatrix()
        translate(200,0)
        rotate(radians(t+2*i*360/90))
        tri(100)
        popMatrix()
    t += 0.5
 
def tri(length):
    noFill()
    triangle(0, -length, -length*sqrt(3)/2, length/2, length*sqrt(3)/2, length/2)
    

my code actually creates rainbow triangle, but im not allowed to use colorMode()

George Profenza
  • 50,687
  • 19
  • 144
  • 218
  • Perhaps the course explained color spaces and provided functions to convert between them (HSB/RGB) ? Otherwise, there are be plenty of resources (ex. [1](https://stackoverflow.com/questions/24852345/hsv-to-rgb-color-conversion), [2](https://gist.github.com/mathebox/e0805f72e7db3269ec22).) – George Profenza Oct 11 '22 at 19:22

1 Answers1

1

See HSL and HSV and create your own function that converts a hue value to an RGB color:

def setup(): 
    size(600,600)
t = 0

def toColor(v):
    return max(0, min(255, v*255))
def hueTotoRgb(hue):
    r = abs(hue * 6 - 3) - 1
    g = 2.0 - abs(hue * 6 - 2)
    b = 2.0 - abs(hue * 6 - 4)
    return (toColor(r), toColor(g), toColor(b))

def draw():
    global t
    background(255)#white
    translate (width/2, height/2)
    for i in range(90):
        stroke(*hueTotoRgb(float(i)/90))
        rotate(radians(360/90))
        pushMatrix()
        translate(200,0)
        rotate(radians(t+2*i*360/90))
        tri(100)
        popMatrix()
    t += 0.5
 
def tri(length):
    noFill()
    triangle(0, -length, -length*sqrt(3)/2, length/2, length*sqrt(3)/2, length/2)

Rabbid76
  • 202,892
  • 27
  • 131
  • 174