# Message-ID: <XnsA8FEF373E5A50PanElektronik@localhost.net>

from math import sqrt
from PIL import Image, ImageDraw

# Image (plate) size
width	= 1024
height	= 768

def calc_distance(x1, y1, x2, y2):
   """Calculates distance between two points."""
   return sqrt(pow(x1 - x2, 2) + pow(y1 - y2, 2))

def calc_color(distance, distmin, distmax):
    """Calculates green color value based on relative (to minimum
    and maximum) distance. Color value is in range 0 to 255."""
    flcolor = (distance - distmin) * 255 / (distmax - distmin)
    return int(flcolor)

def calc_rgb(green):
    """Calculates RGB value for given green color value, maximizing
    color values so yellow is actually yellow."""
    red = 255 - green
    diff = 255 - max(red, green)
    return (red + diff, green + diff, 0)

def draw_point(dr, x, y):
    """Draws one contact point."""
    dr.pieslice((x - 5, y - 5, x + 5, y + 5), 0, 360, (0, 0, 255))

def draw_plate(filename, p1x, p1y, p2x, p2y):
    """Draws one plate."""
    # Create an array consisting of distances between two contact 
    # points for each point of the plate
    distances = []
    for y in range(0, height):
        for x in range(0, width):
            dist1 = calc_distance(x, y, p1x, p1y)
            dist2 = calc_distance(x, y, p2x, p2y)
            distances.append(dist1 + dist2)

    # Find minimum and maximum distance. They will serve as reference values 
    # for color selection (minimum distance will get red color, maximum green)
    distmin	= min(distances)
    distmax	= max(distances)

    im = Image.new("RGB", (width, height))

    # Create plate image
    for y in range(0, height):
        for x in range(0, width):
            distance = distances[y * width + x]
            rgb = calc_rgb(calc_color(distance, distmin, distmax))
            im.putpixel((x, y), rgb)

    # Add contact points
    dr = ImageDraw.Draw(im)
    draw_point(dr, p1x, p1y)
    draw_point(dr, p2x, p2y)
    del dr

    im.save(filename)
    del im

def draw_symmetric_plate(filename, px, py):
    draw_plate(filename, px, py, width - px - 1, height - py - 1)

draw_symmetric_plate("plate1.png", 10, 10)
draw_symmetric_plate("plate2.png", width / 4, height / 4)
draw_plate("plate3.png", 10, 10, width / 2, height / 2)
