#!/usr/bin/python3
#
# Draw a regular n-gon
# Cut it into p equal pieces
# which conain equal amounts of the perimeter
# of the n-gon

from dataclasses import dataclass
from math import pi, cos, sin, modf, sqrt
import sys

@dataclass
class Point():
    x: float
    y: float

#
# Idea: the perimeter is parametrized by
# the total length to some designated start point
# and the side length is assumed to be 1
# So the total perimeter of an n-gon is n,
# with the vertices at positions 0, 1, 2, ..., n-1.
#
# This function converts that parameter to an actual position.
#
# perimeter parameter, hee hee.


# number of sides

if __name__ == '__main__':
    N = int(sys.argv[1])
    pieces = int(sys.argv[2])
else:
    N = 4
    pieces = 9

rot = -pi/N if N % 2 == 0 else  -pi/2
off = 0.0

def seg_length(p1, p2):
    return sqrt((p1.x - p2.x)**2 +
                (p1.y - p2.y)**2)

def perimeter_position(parameter):
    (f, i) = modf(parameter)
    (a, b) = endpoints_of_side(int(i))
#    print("pp", parameter, (i, f), "\n\t", a, b, file=sys.stderr)
    z = between(a, b, f)
#    print("  *", z, file=sys.stderr)
    return z

def between(a, b, f):
    # The point 'f' of the way from a to b,
    # where f is a fraction between 0 and 1.
    # If f = 0, retuns a; if f = 1 returns b.
    # If f = 0.5 returns the midpoint.
    return Point(a.x * (1-f) + b.x * f,
                 a.y * (1-f) + b.y * f)

# position of a vertex
def vertex(vertex_no):
    return Point(cos(vertex_no/N * 2 * pi + rot),
                 sin(vertex_no/N * 2 * pi + rot))

def pmod(a, b, m):
    return (a+b) % m

def endpoints_of_side(side_no):
    return (vertex(side_no), vertex(pmod(side_no, 1, N)))

def cut_points(pieces):
    return [ perimeter_position(pmod(i * N / pieces, off, N))
             for i in range(pieces) ]

# print([ vertex(i) for i in range(N) ])

print('<svg viewBox="0 0 400 400" xmlns="http://www.w3.org/2000/svg">')
print('<g transform="scale(200) translate(1 1)" stroke-width="0.002">')
print('<polygon fill="white" stroke="black" points="', end="")
for p in [ vertex(i) for i in range(N) ]:
    print(f"{p.x :.2f},{p.y :.2f}", end=" ")
print('"/>')
for p in cut_points(pieces):
    print(f'<line x1="0" y1="0" x2="{p.x}" y2="{p.y}" stroke="green" />')

print("</g>")
print("</svg>")
