import svgwrite
import math

svg_document = svgwrite.Drawing(filename = "using-svgwrite.svg",
size = ("100px", "100px")) 


'''
A pattern of duochrome uninterrupted diagonal lines, numX by numY
'''
def lines(numX, numY, xOffset, yOffset, xMultiplier, yMultiplier, lengthX, lengthY, colorA, colorB):
    x = 0
    while x < numX:
        y = 0
        while y < numY:
            xStart = x*xMultiplier+xOffset
            xEnd   = xStart+lengthX
            yStart = y*yMultiplier+yOffset
            yEnd   = yStart+lengthY
            # A line with color A
            lineA = svg_document.line((xStart, yStart), (xEnd, yEnd), stroke_width = 1, stroke = colorA)
            svg_document.add(lineA)
            # A line with color B
            lineB = svg_document.line((xStart+1, yStart), (xEnd+1, yEnd), stroke_width = 1, stroke = colorB)
            svg_document.add(lineB)
            y += 1
        x += 1


'''
A duochrome four-leaf clover pattern with outline, where the pattern extends inwards like a maelstrom
'''
def fourLeafCloverHalfSwirl(numX, numY, iOffset, jOffset, iSpacing, jSpacing, colorA, colorB):
    baseR = 2
    bigR = baseR*1
    i = 0
    while i < numX:
        j = 0
        while j < numY:
            svg_document.add(
                svg_document.circle(
                    center=(i*iSpacing+iOffset-baseR,j*jSpacing+jOffset), 
                    r=2,
                    stroke_width = "0.5",
                    stroke = colorB,
                    fill = colorA))
            svg_document.add(
                svg_document.circle(
                    center=(i*iSpacing+iOffset,j*jSpacing+jOffset-baseR), 
                    r=2,
                    stroke_width = "0.5",
                    stroke = colorB,
                    fill = colorA))
            svg_document.add(
                svg_document.circle(
                    center=(i*iSpacing+iOffset+baseR,j*jSpacing+jOffset), 
                    r=2,
                    stroke_width = "0.5",
                    stroke = colorB,
                    fill = colorA))
            svg_document.add(
                svg_document.circle(
                    center=(i*iSpacing+iOffset,j*jSpacing+jOffset+baseR), 
                    r=2,
                    stroke_width = "0.5",
                    stroke = colorB,
                    fill = colorA))
            svg_document.add(
                svg_document.circle(
                    center=(i*iSpacing+iOffset,j*jSpacing+jOffset), 
                    r=bigR,
                    fill = colorA))
            j += 1
        i += 1


'''
A duochrome four-leaf clover pattern with outline, where the pattern extends inwards on the vertical axis
'''
def fourLeafCloverVert(numX, numY, iOffset, jOffset, iSpacing, jSpacing, colorA, colorB):
    r = 2
    i = 0
    while i < numX:
        j = 0
        while j < numY:
            baseI = i*iSpacing+iOffset;
            baseJ = j*jSpacing+jOffset;
            svg_document.add(
                svg_document.circle(
                    center=(baseI-r,baseJ), 
                    r=r,
                    stroke_width = 0.5,
                    stroke = colorB,
                    fill = colorA))
            svg_document.add(
                svg_document.circle(
                    center=(baseI+r,baseJ), 
                    r=r,
                    stroke_width = 0.5,
                    stroke = colorB,
                    fill = colorA))
            svg_document.add(
                svg_document.circle(
                    center=(baseI,baseJ-r), 
                    r=r,
                    stroke_width = 0.5,
                    stroke = colorB,
                    fill = colorA))
            svg_document.add(
                svg_document.circle(
                    center=(baseI,baseJ+r), 
                    r=r,
                    stroke_width = 0.5,
                    stroke = colorB,
                    fill = colorA))
            svg_document.add(
                svg_document.circle(
                    center=(baseI,baseJ), 
                    r=r,
                    fill = colorA))
            j += 1
        i += 1



'''
A duochrome four-leaf clover pattern with outline, where the pattern extends inwards on the horizontal axis
'''
def fourLeafCloverHor(numX, numY, iOffset, jOffset, iSpacing, jSpacing, colorA, colorB):
    r = 2
    i = 0
    while i < numX:
        j = 0
        while j < numY:
            baseI = i*iSpacing+iOffset;
            baseJ = j*jSpacing+jOffset;
            svg_document.add(
                svg_document.circle(
                    center=(baseI,baseJ-r), 
                    r=r,
                    stroke_width = 0.5,
                    stroke = colorB,
                    fill = colorA))
            svg_document.add(
                svg_document.circle(
                    center=(baseI,baseJ+r), 
                    r=r,
                    stroke_width = 0.5,
                    stroke = colorB,
                    fill = colorA))
            svg_document.add(
                svg_document.circle(
                    center=(baseI-r,baseJ), 
                    r=r,
                    stroke_width = 0.5,
                    stroke = colorB,
                    fill = colorA))
            svg_document.add(
                svg_document.circle(
                    center=(baseI+r,baseJ), 
                    r=r,
                    stroke_width = 0.5,
                    stroke = colorB,
                    fill = colorA))
            svg_document.add(
                svg_document.circle(
                    center=(baseI,baseJ), 
                    r=r,
                    fill = colorA))
            j += 1
        i += 1

baseColor = "rgb(250,250,250)"
colorA = "rgb(50,155,50)"
colorB = "rgb(25,100,25)"

svg_document.add(svg_document.rect(insert = (0, 0),
    size = ("100px", "100px"),
    fill = baseColor))

lineLength = 25
singleSpacing = 25
doubleSpacing = 2 * singleSpacing
tripleSpacing = 3 * singleSpacing

# First and third row of lines, from top-left to bottom-right
lines(3, 2, 0, 0, singleSpacing, doubleSpacing, lineLength, lineLength, colorA, colorB)
# Second and fourth row of lines, from top-left to bottom-right
lines(3, 2, 0, lineLength, singleSpacing, doubleSpacing, lineLength, lineLength, colorA, colorB)
# First and third row of lines, from top-right to bottom-left.
# We add a line-length as offset because we use a negative linelength for Y to flip the direction of the line
lines(3, 2, 0, lineLength, singleSpacing, doubleSpacing, lineLength, -lineLength, colorB, colorA)
# Second and fourth row of lines, from top-right to bottom-left
lines(3, 2, 0, 2*lineLength, singleSpacing, doubleSpacing, lineLength, -lineLength, colorB, colorA)

# First and third row of decorations
fourLeafCloverVert(3, 2, 0, 0, singleSpacing, doubleSpacing, colorA, colorB)
# Second and fourth rows of decorations
# Give a vertical offset of singleSpacing, so that the second type of decoration will fall between the first 
fourLeafCloverHor      (3, 1, 0, singleSpacing, singleSpacing, doubleSpacing, colorA, colorB)
fourLeafCloverHalfSwirl(3, 1, 0, tripleSpacing, singleSpacing, doubleSpacing, colorA, colorB)

svg_document.save() 
