This program uses vector mathematics and colors to create triangular gradient. By simply left-clicking you create three points which then will be filled in with color.

Controls

• Left click adds a new position
• Space saves a screenshot

``````# Python 2.7.7 Code
# Pygame 1.9.1 (for Python 2.7.7)
# Jonathan Frech  6th of October, 2015
#         edited  9th of October, 2015
#         edited 10th of October, 2015``````

``````# importing needed modules
import pygame, sys, time, math, os, random, datetime

""" CLASSES """
# dummy class for global variables
class dummy():
pass

""" FUNCTIONS """
# validates color integer
# extra feature: _min and _max implementation
def colorValid(_color, _min = 0, _max = 255):
newColor = math.fabs(_color)
n = _max - _min
if newColor > n:
if int(newColor / n) % 2 == 0:
newColor = newColor % n
else:
newColor = n - (newColor % n)

return int(newColor) + _min

# saves the current surface
def saveSurface():
try:
path = os.getcwd() + "/Gradient Triangles/"
if not os.path.isdir(path):
os.mkdir(path)

name = path + "img" + str(len(os.listdir(path))) + ".png"
pygame.image.save(main.SURF, name)
except:
pass

# basic vector functions
def vecConvert(p1, p2):
return [p2[0] - p1[0], p2[1] - p1[1]]
def vecLen(vec):
return math.sqrt( (vec[0]**2) + (vec[1]**2) )
def vecMultiply(vec, n):
return [vec[0] * n, vec[1] * n]
def vecGetPoint(vec, point):
return [point[0] + vec[0], point[1] + vec[1]]
return [vec1[0] + vec2[0], vec1[1] + vec2[1]]

# quits the program
def quit():
sys.exit()

""" TICK; RENDER """
# tick function
def tick():
# handle events
for event in pygame.event.get():
# quit
if event.type == pygame.QUIT:
quit()

# keydown
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
saveSurface()

# mousebuttondown
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:

# add a position to main.POINTS
main.POINTS.append(pygame.mouse.get_pos())

# if you have more or equal than three positions, draw the triangle
if len(main.POINTS) >= 3:
# defines the line's density
N = 10000

# variables
points02 = []
points12 = []
vec02 = vecConvert(main.POINTS[0], main.POINTS[2])
vec12 = vecConvert(main.POINTS[1], main.POINTS[2])
vecl02 = vecLen(vec02)
vecl12 = vecLen(vec12)

# creates the first set of points
for _ in range(0, N):
if vecl02 != 0:
vecn = vecMultiply(vec02, vecl02 / N * _ / vecl02)
points02.append(vecGetPoint(vecn, main.POINTS[0]))

# creates the second set of points
for _ in range(0, N):
if vecl12 != 0:
vecn = vecMultiply(vec12, vecl12 / N * _ / vecl12)
points12.append(vecGetPoint(vecn, main.POINTS[1]))

# connects the points and chooses a color
l = len(points02)
for _ in range(0, l):
# define the color
#c = [random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)]
#"""
c = [
#255. / l * _,
#255. / l * _,
#0

#255. / l * _,
#0,
#255 - 255. / l * _

255. / l * _,
255 - 255. / l * _,
0
]#"""

# refine the color
c = [int(c[0]), int(c[1]), int(c[2])]

# draw a line
pygame.draw.line(main.SURF, c, points02[_], points12[_])

# clear the points
main.POINTS = []

# render function
def render():
# blit and flip
main.SCREEN.blit(main.SURF, [0, 0])

# draw the current points
for _ in main.POINTS:
pygame.draw.circle(main.SCREEN, [255, 255, 255], _, 3)

pygame.display.flip()

""" INIT """
# initialize program
def init():
main.WIDTH, main.HEIGHT = 1080, 720
main.SIZE = [main.WIDTH, main.HEIGHT]
main.SCREEN = pygame.display.set_mode(main.SIZE)
main.SURF = pygame.Surface(main.SIZE)

main.CAPTION = "Gradient Triangles"
main.TICKS = 0

main.POINTS = []

# functions
pygame.display.set_caption(main.CAPTION)

""" RUN """
# run function (uses tick() and render())
def run():
ticksPerSecond = 60
lastTime = time.time() * 1000000000
nsPerTick =  1000000000.0 / float(ticksPerSecond)

ticks = 0
frames = 0

lastTimer = time.time() * 1000
delta = 0.0

while True:
now = time.time() * 1000000000
delta += float(now - lastTime) / float(nsPerTick)
lastTime = now
shouldRender = False

while delta >= 1:
ticks += 1
main.TICKS += 1
tick()
delta -= 1
shouldRender = True

if shouldRender:
frames += 1
render()

if time.time() * 1000 - lastTimer >= 1000:
lastTimer += 1000

# debug
# print("Frames: " + str(frames) + ", ticks: " + str(ticks))

frames = 0
ticks = 0

# main variable
main = dummy()
init()

# start program
run()``````