
import pygame
from helpers import Point,Triangle
    
black = pygame.Color("black")
green = pygame.Color("green")
blue = pygame.Color("blue")
yellow = pygame.Color("yellow")
red = pygame.Color("red")
magenta = pygame.Color("magenta")
cyan = pygame.Color("cyan")

def process_events():
    for event in pygame.event.get():
        if event.type == pygame.QUIT: 
            pygame.quit()
            return True
    return False

def showTriang(polygon,triangles=[], start=None, end=None, path=None, debug_triangles=None):
    """
    Ocekavame, ze polygon je posloupnost dvojic bodu
    Triangles je seznam trojic bodu, ktere tvori samostatne trojuhelniky
    Vykresli i stredovy bod, pokud je pritomen
    """
    global screen 
    scalex = 1
    scaley = 1
    shiftx = 0
    shifty = 0

    xs = [ p.x for p in polygon ]
    ys = [ p.y for p in polygon ]
    if start:
        xs.append(start.x)
        ys.append(start.y)
    if end:
        xs.append(end.x)
        ys.append(end.y)

    maxx = max(xs)
    maxy = max(ys)
    minx = min(xs)
    miny = min(ys)

    height = width = 900

    if maxx > width:
        shiftx = minx
        scalex = width/float(maxx - minx) 

    if maxy > height:
        shifty = miny
        scaley = height/float(maxy - miny) 

    plg = [ ((p.x-shiftx)*scalex, width-(p.y-shifty)*scaley) for p in polygon ]
    while 1:
        if process_events():
            return
        screen.fill(black)
        pygame.draw.polygon(screen, green, plg)
        for tr in triangles:
            trg = [ ((tr.p1.x-shiftx)*scalex, height-(tr.p1.y-shifty)*scaley), ((tr.p2.x-shiftx)*scalex, height-(tr.p2.y-shifty)*scaley), ((tr.p3.x-shiftx)*scalex, height-(tr.p3.y-shifty)*scaley) ]
            pygame.draw.polygon(screen, magenta, trg, 2)
            if tr.center:
                pygame.draw.circle(screen, yellow, ((tr.center.x-shiftx)*scalex, height-(tr.center.y-shifty)*scaley), 2)
        if start:
            pygame.draw.circle(screen, red, ((start.x-shiftx)*scalex, height-(start.y-shifty)*scaley), 2)
        if end:
            pygame.draw.circle(screen, red, ((end.x-shiftx)*scalex, height-(end.y-shifty)*scaley), 2)
        if path:
            plist = [ ((p.x-shiftx)*scalex, height-(p.y-shifty)*scaley) for p in path ]
            pygame.draw.lines(screen, blue, False, plist, 3)

        for dtr in debug_triangles:
            dtrg = [ ((dtr.p1.x-shiftx)*scalex, height-(dtr.p1.y-shifty)*scaley), ((dtr.p2.x-shiftx)*scalex, height-(dtr.p2.y-shifty)*scaley), ((dtr.p3.x-shiftx)*scalex, height-(dtr.p3.y-shifty)*scaley) ]
            pygame.draw.polygon(screen, blue, dtrg, 2)

        pygame.display.flip()


def init():
    global screen 
    pygame.init()
    width = 900 
    height = 900
    screen = pygame.display.set_mode((width,height))
    


if __name__=="__main__":
    init()

    p=[(2053548,6362005), (2053441,6361980), (2053228,6361922), (2053200,6361860), (2053115,6361853), (2052941,6361796), (2052916,6361802), (2052894,6361822), (2052612,6361772), (2052551,6361797), (2052472,6361787), (2052478,6361763), (2052466,6361753), (2052435,6361765), (2051907,6361663), (2051198,6361751), (2051096,6361756), (2051044,6361725), (2051176,6361566), (2051234,6361399), (2051220,6361299), (2051223,6361222), (2051216,6361133), (2051178,6361075), (2051122,6360892), (2051129,6360847), (2051083,6360823), (2051057,6360757), (2051058,6360711), (2051115,6360620), (2051128,6360586), (2051149,6360564), (2051141,6360546), (2051076,6360574), (2051055,6360567), (2051041,6360432), (2051126,6360023), (2051137,6359934), (2051132,6359828), (2051112,6359667), (2051048,6359533), (2050977,6359365), (2050958,6359278), (2050961,6359203), (2051032,6359091), (2051093,6359035), (2051185,6358930), (2051220,6358844), (2051255,6358779), (2051280,6358682), (2051271,6358584), (2051371,6358634), (2051389,6358651), (2051466,6358693), (2051603,6358831), (2051624,6358838), (2051651,6358861), (2051658,6358853), (2051688,6358872), (2051702,6358914), (2051722,6358921), (2051813,6359030), (2051828,6359020), (2051748,6358911), (2051749,6358883), (2051573,6358558), (2051354,6358362), (2051340,6358308), (2051364,6358238), (2051362,6358230), (2051422,6358080), (2051451,6358039), (2051509,6357998), (2051557,6357993), (2051615,6357996), (2051695,6357963), (2051753,6357989), (2051767,6358004), (2051810,6358001), (2051917,6358034), (2051943,6358036), (2051962,6358023), (2051993,6358013), (2052029,6357983), (2052065,6357948), (2052059,6357941), (2052008,6357982), (2051910,6357980), (2051662,6357786), (2051636,6357741), (2051645,6357684), (2051658,6357655), (2051682,6357635), (2051729,6357546), (2051739,6357518), (2051772,6357471), (2051805,6357451), (2051876,6357356), (2052013,6357212), (2052053,6357189), (2052083,6357140), (2052120,6357093), (2052129,6357034), (2052177,6356933), (2052185,6356865), (2052212,6356775), (2052206,6356615), (2052220,6356470), (2052240,6356360), (2052247,6356190), (2052238,6356128), (2052236,6356017), (2052253,6355909), (2052284,6355836), (2052353,6355682), (2052385,6355632), (2052433,6355600), (2052496,6355598), (2052524,6355579), (2052536,6355584), (2052539,6355596), (2052547,6355603), (2052544,6355641), (2052536,6355670), (2052553,6355699), (2052564,6355738), (2052581,6355754), (2052591,6355745), (2052572,6355647), (2052586,6355612), (2052581,6355576), (2052596,6355545), (2052622,6355515), (2052655,6355511), (2052694,6355461), (2052725,6355459), (2052777,6355429), (2052901,6355295), (2052927,6355238), (2052906,6355187), (2052902,6355135), (2052943,6355108), (2052977,6355098), (2052969,6355079), (2052996,6355033), (2052963,6354952), (2052971,6354940), (2052996,6354946), (2053006,6354924), (2052999,6354856), (2053021,6354833), (2053017,6354807), (2053034,6354761), (2053024,6354740), (2053034,6354692), (2053045,6354658), (2053014,6354498), (2052989,6354483), (2053050,6354317), (2053068,6354305), (2053090,6354250), (2053073,6354241), (2053086,6354221), (2053102,6354214), (2053126,6354188), (2053142,6354179), (2053150,6354158), (2053213,6354139), (2053253,6354143), (2053274,6354125), (2053272,6354117), (2053269,6354109), (2053124,6354117), (2053109,6354112), (2053057,6354118), (2053005,6354111), (2052963,6354110), (2052922,6354150), (2052903,6354197), (2052876,6354201), (2052867,6354233), (2052853,6354234), (2052844,6354220), (2052827,6354210), (2052811,6354222), (2052793,6354259), (2052752,6354280), (2052745,6354305), (2052749,6354360), (2052732,6354404), (2052722,6354410), (2052714,6354425), (2052713,6354463), (2052703,6354465), (2052697,6354506), (2052683,6354548), (2052669,6354609), (2052655,6354601), (2052622,6354612), (2052584,6354667), (2052563,6354708), (2052548,6354728), (2052529,6354700), (2052516,6354630), (2052491,6354609), (2052468,6354600), (2052454,6354607), (2052460,6354626), (2052460,6354642), (2052468,6354658), (2052486,6354676), (2052497,6354698), (2052498,6354716), (2052504,6354722), (2052507,6354732), (2052503,6354736), (2052482,6354731), (2052469,6354735), (2052475,6354740), (2052475,6354751), (2052484,6354755), (2052487,6354766), (2052493,6354768), (2052497,6354777), (2052509,6354780), (2052512,6354790), (2052523,6354796), (2052532,6354810), (2052532,6354822), (2052562,6354847), (2052559,6354889), (2052579,6354969), (2052560,6355021), (2052553,6355073), (2052456,6355154), (2052410,6355161), (2052334,6355208), (2052188,6355268), (2052110,6355303), (2052073,6355357), (2052035,6355460), (2051988,6355438), (2051978,6355370), (2051936,6355294), (2051899,6355173), (2051877,6355099), (2051863,6355101), (2051879,6355180), (2051869,6355187), (2051876,6355267), (2051877,6355309), (2051889,6355330), (2051895,6355353), (2051915,6355371), (2051924,6355405), (2051936,6355425), (2051934,6355435), (2051941,6355458), (2051947,6355468), (2051954,6355491), (2051998,6355514), (2052014,6355544), (2052022,6355579), (2052030,6355729), (2052050,6355880), (2052049,6355949), (2052019,6356095), (2051952,6356237), (2051937,6356258), (2051904,6356340), (2051838,6356336), (2051780,6356283), (2051718,6356142), (2051663,6356058), (2051632,6355922), (2051599,6355824), (2051480,6355693), (2051461,6355625), (2051395,6355553), (2051381,6355584), (2051357,6355581), (2051352,6355609), (2051329,6355629), (2051353,6355638), (2051413,6355703), (2051424,6355743), (2051454,6355837), (2051445,6355890), (2051434,6355912), (2051456,6355945), (2051475,6355980), (2051487,6356110), (2051536,6356211), (2051521,6356266), (2051539,6356304), (2051627,6356393), (2051685,6356468), (2051788,6356652), (2051823,6356691), (2051831,6356740), (2051772,6356900), (2051686,6357018), (2051600,6357092), (2051474,6357129), (2051334,6357108), (2051034,6356972), (2051024,6357001), (2050842,6357006), (2050791,6357034), (2050749,6357028), (2050750,6357052), (2050777,6357070), (2050826,6357074), (2050874,6357071), (2050929,6357096), (2050945,6357107), (2050956,6357106), (2050991,6357147), (2051008,6357180), (2050994,6357202), (2051009,6357260), (2050999,6357296), (2050985,6357326), (2050907,6357425), (2050868,6357458), (2050830,6357519), (2050774,6357534), (2050759,6357565), (2050771,6357585), (2050745,6357702), (2050745,6357838), (2050738,6357853), (2050755,6357875), (2050755,6357887), (2050752,6357909), (2050758,6357926), (2050777,6357962), (2050785,6358014), (2050803,6358046), (2050814,6358086), (2050816,6358108), (2050836,6358169), (2050883,6358282), (2050901,6358348), (2050903,6358391), (2050881,6358493), (2050864,6358497), (2050850,6358482), (2050703,6358436), (2050682,6358421), (2050667,6358389), (2050640,6358393), (2050598,6358380), (2050556,6358343), (2050527,6358336), (2050507,6358319), (2050481,6358296), (2050462,6358291), (2050455,6358304), (2050473,6358323), (2050484,6358340), (2050506,6358342), (2050544,6358373), (2050605,6358437), (2050635,6358476), (2050632,6358496), (2050639,6358513), (2050676,6358546), (2050718,6358586), (2050747,6358606), (2050774,6358627), (2050794,6358715), (2050777,6358761), (2050684,6358886), (2050634,6358929), (2050592,6358986), (2050559,6359014), (2050471,6359121), (2050418,6359203), (2050358,6359231), (2050339,6359237), (2050309,6359253), (2050276,6359259), (2050277,6359270), (2050298,6359265), (2050326,6359270), (2050344,6359289), (2050394,6359319), (2050414,6359428), (2050426,6359700), (2050441,6359729), (2050443,6359806), (2050463,6359839), (2050466,6359869), (2050472,6359866), (2050479,6359807), (2050490,6359832), (2050491,6359844), (2050505,6359842), (2050514,6359863), (2050547,6359882), (2050528,6359917), (2050508,6359916), (2050512,6359931), (2050486,6359924), (2050480,6359935), (2050512,6359965), (2050497,6359981), (2050489,6360015), (2050483,6360028), (2050494,6360033), (2050514,6360021), (2050541,6360000), (2050573,6359993), (2050602,6359996), (2050611,6360009), (2050605,6360049), (2050616,6360097), (2050617,6360186), (2050609,6360218), (2050605,6360392), (2050614,6360454), (2050618,6360645), (2050628,6360688), (2050627,6360775), (2050621,6360804), (2050647,6360921), (2050649,6360980), (2050691,6361039), (2050668,6361066), (2050661,6361119), (2050671,6361144), (2050666,6361181), (2050657,6361210), (2050652,6361212), (2050645,6361258), (2050655,6361295), (2050343,6361321), (2050248,6361360), (2050108,6361472), (2049960,6361631), (2053587,6362067), (2053590,6362049), (2053581,6362023), (2053548,6362005)]
    p1=[(2051044,6361725), (2051176,6361566), (2051234,6361399), (2051220,6361299), (2051223,6361222), (2051216,6361133), (2051178,6361075), (2051122,6360892), (2051129,6360847), (2051083,6360823), (2051057,6360757), (2051058,6360711), (2051115,6360620), (2051128,6360586), (2051149,6360564), (2051141,6360546), (2051076,6360574), (2051055,6360567), (2051041,6360432), (2051126,6360023), (2051137,6359934), (2051132,6359828), (2051112,6359667), (2051048,6359533), (2050977,6359365), (2050958,6359278), (2050961,6359203), (2051032,6359091), (2051093,6359035), (2051185,6358930), (2051220,6358844), (2051255,6358779), (2051280,6358682), (2051271,6358584), (2051371,6358634), (2051389,6358651), (2051466,6358693), (2051603,6358831), (2051624,6358838), (2051651,6358861), (2051658,6358853), (2051688,6358872), (2051702,6358914), (2051722,6358921), (2051813,6359030), (2051828,6359020), (2051748,6358911), (2051749,6358883), (2051573,6358558), (2051354,6358362), (2051340,6358308), (2051364,6358238), (2051362,6358230), (2051422,6358080), (2051451,6358039), (2051509,6357998), (2051557,6357993), (2051615,6357996), (2051695,6357963), (2051753,6357989), (2051767,6358004), (2051810,6358001), (2051917,6358034), (2051943,6358036), (2051962,6358023), (2051993,6358013), (2052029,6357983), (2052065,6357948), (2052059,6357941), (2052008,6357982), (2051910,6357980), (2051662,6357786), (2051636,6357741), (2051645,6357684), (2051658,6357655), (2051682,6357635), (2051729,6357546), (2051739,6357518), (2051772,6357471), (2051805,6357451), (2051876,6357356), (2052013,6357212), (2052053,6357189), (2052083,6357140), (2052120,6357093), (2052129,6357034), (2052177,6356933), (2052185,6356865), (2052212,6356775), (2052206,6356615), (2052220,6356470), (2052240,6356360), (2052247,6356190), (2052238,6356128), (2052236,6356017), (2052253,6355909), (2052284,6355836), (2052353,6355682), (2052385,6355632), (2052433,6355600), (2052496,6355598), (2052524,6355579), (2052536,6355584), (2052539,6355596), (2052547,6355603), (2052544,6355641), (2052536,6355670), (2052553,6355699), (2052564,6355738), (2052581,6355754), (2052591,6355745), (2052572,6355647), (2052586,6355612), (2052581,6355576), (2052596,6355545), (2052622,6355515), (2052655,6355511), (2052694,6355461), (2052725,6355459), (2052777,6355429), (2052901,6355295), (2052927,6355238), (2052906,6355187), (2052902,6355135), (2052943,6355108), (2052977,6355098), (2052969,6355079), (2052996,6355033), (2052963,6354952), (2052971,6354940), (2052996,6354946), (2053006,6354924), (2052999,6354856), (2053021,6354833), (2053017,6354807), (2053034,6354761), (2053024,6354740), (2053034,6354692), (2053045,6354658), (2053014,6354498), (2052989,6354483), (2053050,6354317), (2053068,6354305), (2053090,6354250), (2053073,6354241), (2053086,6354221), (2053102,6354214), (2053126,6354188), (2053142,6354179), (2053150,6354158), (2053213,6354139), (2053253,6354143), (2053274,6354125), (2053272,6354117), (2053269,6354109), (2053124,6354117), (2053109,6354112), (2053057,6354118), (2053005,6354111), (2052963,6354110), (2052922,6354150), (2052903,6354197), (2052876,6354201), (2052867,6354233), (2052853,6354234), (2052844,6354220), (2052827,6354210), (2052811,6354222), (2052793,6354259), (2052752,6354280), (2052745,6354305), (2052749,6354360), (2052732,6354404), (2052722,6354410), (2052714,6354425), (2052713,6354463), (2052703,6354465), (2052697,6354506), (2052683,6354548), (2052669,6354609), (2052655,6354601), (2052622,6354612), (2052584,6354667), (2052563,6354708), (2052548,6354728), (2052529,6354700), (2052516,6354630), (2052491,6354609), (2052468,6354600), (2052454,6354607), (2052460,6354626), (2052460,6354642), (2052468,6354658), (2052486,6354676), (2052497,6354698), (2052498,6354716), (2052504,6354722), (2052507,6354732), (2052503,6354736), (2052482,6354731), (2052469,6354735), (2052475,6354740), (2052475,6354751), (2052484,6354755), (2052487,6354766), (2052493,6354768), (2052497,6354777), (2052509,6354780), (2052512,6354790), (2052523,6354796), (2052532,6354810), (2052532,6354822), (2052562,6354847), (2052559,6354889), (2052579,6354969), (2052560,6355021), (2052553,6355073), (2052456,6355154), (2052410,6355161), (2052334,6355208), (2052188,6355268), (2052110,6355303), (2052073,6355357), (2052035,6355460), (2051988,6355438), (2051978,6355370), (2051936,6355294), (2051899,6355173), (2051877,6355099), (2051863,6355101), (2051879,6355180), (2051869,6355187), (2051876,6355267), (2051877,6355309), (2051889,6355330), (2051895,6355353), (2051915,6355371), (2051924,6355405), (2051936,6355425), (2051934,6355435), (2051941,6355458), (2051947,6355468), (2051954,6355491), (2051998,6355514), (2052014,6355544), (2052022,6355579), (2052030,6355729), (2052050,6355880), (2052049,6355949), (2052019,6356095), (2051952,6356237), (2051937,6356258), (2051904,6356340), (2051838,6356336), (2051780,6356283), (2051718,6356142), (2051663,6356058), (2051632,6355922), (2051599,6355824), (2051480,6355693), (2051461,6355625), (2051395,6355553), (2051381,6355584), (2051357,6355581), (2051352,6355609), (2051329,6355629), (2051353,6355638), (2051413,6355703), (2051424,6355743), (2051454,6355837), (2051445,6355890), (2051434,6355912), (2051456,6355945), (2051475,6355980), (2051487,6356110), (2051536,6356211), (2051521,6356266), (2051539,6356304), (2051627,6356393), (2051685,6356468), (2051788,6356652), (2051823,6356691), (2051831,6356740), (2051772,6356900), (2051686,6357018), (2051600,6357092), (2051474,6357129), (2051334,6357108), (2051034,6356972), (2051024,6357001), (2050842,6357006), (2050791,6357034), (2050749,6357028), (2050750,6357052), (2050777,6357070), (2050826,6357074), (2050874,6357071), (2050929,6357096), (2050945,6357107), (2050956,6357106), (2050991,6357147), (2051008,6357180), (2050994,6357202), (2051009,6357260), (2050999,6357296), (2050985,6357326), (2050907,6357425), (2050868,6357458), (2050830,6357519), (2050774,6357534), (2050759,6357565), (2050771,6357585), (2050745,6357702), (2050745,6357838), (2050738,6357853), (2050755,6357875), (2050755,6357887), (2050752,6357909), (2050758,6357926), (2050777,6357962), (2050785,6358014), (2050803,6358046), (2050814,6358086), (2050816,6358108), (2050836,6358169), (2050883,6358282), (2050901,6358348), (2050903,6358391), (2050881,6358493), (2050864,6358497), (2050850,6358482), (2050703,6358436), (2050682,6358421), (2050667,6358389), (2050640,6358393), (2050598,6358380), (2050556,6358343), (2050527,6358336), (2050507,6358319), (2050481,6358296), (2050462,6358291), (2050455,6358304), (2050473,6358323), (2050484,6358340), (2050506,6358342), (2050544,6358373), (2050605,6358437), (2050635,6358476), (2050632,6358496), (2050639,6358513), (2050676,6358546), (2050718,6358586), (2050747,6358606), (2050774,6358627), (2050794,6358715), (2050777,6358761), (2050684,6358886), (2050634,6358929), (2050592,6358986), (2050559,6359014), (2050471,6359121), (2050418,6359203), (2050358,6359231), (2050339,6359237), (2050309,6359253), (2050276,6359259), (2050277,6359270), (2050298,6359265), (2050326,6359270), (2050344,6359289), (2050394,6359319), (2050414,6359428), (2050426,6359700), (2050441,6359729), (2050443,6359806), (2050463,6359839), (2050466,6359869), (2050472,6359866), (2050479,6359807), (2050490,6359832), (2050491,6359844), (2050505,6359842), (2050514,6359863), (2050547,6359882), (2050528,6359917), (2050508,6359916), (2050512,6359931), (2050486,6359924), (2050480,6359935), (2050512,6359965), (2050497,6359981), (2050489,6360015), (2050483,6360028), (2050494,6360033), (2050514,6360021), (2050541,6360000), (2050573,6359993), (2050602,6359996), (2050611,6360009), (2050605,6360049), (2050616,6360097), (2050617,6360186), (2050609,6360218), (2050605,6360392), (2050614,6360454), (2050618,6360645), (2050628,6360688), (2050627,6360775), (2050621,6360804), (2050647,6360921), (2050649,6360980), (2050691,6361039), (2050668,6361066), (2050661,6361119), (2050671,6361144), (2050666,6361181), (2050657,6361210), (2050652,6361212), (2050645,6361258), (2050655,6361295), (2050343,6361321), (2050248,6361360), (2050108,6361472)]
    p2=[(2049960,6361631), (2053587,6362067), (2053590,6362049), (2053581,6362023), (2053548,6362005), (2053548,6362005), (2053441,6361980), (2053228,6361922), (2053200,6361860), (2053115,6361853), (2052941,6361796), (2052916,6361802), (2052894,6361822), (2052612,6361772), (2052551,6361797), (2052472,6361787), (2052478,6361763), (2052466,6361753), (2052435,6361765), (2051907,6361663), (2051198,6361751), (2051096,6361756), (2051044,6361725)]

    plg = [ Point(x,y) for (x,y) in p2 ]
    showTriang(plg)
#    showTriang(
#        [Point(190,100), Point(220,240), 
#         Point(100,300), Point(340,430), 
#         Point(500,220), Point(430,50), 
#         Point(340,170) 
#        ],
#        [Triangle(Point(190,100), Point(220,240), Point(340,430))
#        ]
#         )
#    showTriang(
#        [(190,100), (220,240), 
#         (100,300), (340,430), 
#         (500,220), (430,50), 
#         (340,170) 
#        ],
#        [((190,100), (220,240), (340,430))
#        ]
#         )

# vi: ts=4:sw=4:expandtab:
