jack: (Default)
[personal profile] jack
I'd meant to do something else this weekend, but I spend a lot of time on a different project. I reimplemented a lot of Mark Dominus' linogram in Python.

I haven't quite decided on the right syntax yet, you can either say constrain( equation_i_want_to_be_true ) or you can use Obj objects like Point, Line, Circle, etc where normal assignment syntax expresses a new constraint.

Behind the scenes it caches each constraint you express, and solves them as a big set of linear equations that it tries to keep in a solved state, dividing them into "variables it knows the answer to" which you can read at any time, and "variables yet to be determined". It rejects new constraints that produce contradictions.

An example of use to solve a set of linear equations would be:
    from pylogram import constrain, default_vars as vars
    constrain(   vars.a + vars.b   == 2  )
    constrain( 2*vars.a + vars.b/3 == -7 )
    print( "a , b =", vars.a, ",", vars.b )

Which gives output:
    >> -23/5 , 33/5

An example of geometric drawing would be:
    from pylogram_draw import *
    
    class Lollypop(Obj):
        def __init__(self):
            self.stick = Line('stick')
            self.circ = Circle('ball')
            self.circ.bottom = self.stick.pt1
            self.stick.pt1.x = self.stick.pt2.x
            line_height = self.stick.pt2.y - self.stick.pt1.y
            line_height [:]= self.circ.d * 2
            self.bottom = self.stick.pt2
            self.height = line_height + self.circ.d
    
    lollypop = Lollypop()
    lollypop.bottom = Point(1,0)
    lollypop.height = 6
    
    assert lollypop.circ.c == Point(1,-5)
    lollypop.sim_draw()

Which gives output:
  
    >> Drawing circle about 1,-5 with radius 1
    >> Drawing line from 1,-4 to 1,0

Actual drawing isn't implemented yet.

A Contradiction exception is thrown if you add a contradictory condition, for instance:
    from pylogram_draw import *
    
    pt = Point()
    pt.x = pt.y + 1
    pt.x = 2
    # pt.y = 57 # <-- Exception thrown on the third assignment, regardless of the order

Source code on github: https://github.com/CartesianDaemon/pylogram

Unsurprisingly this decade someone got to jackv before me. I couldn't decide if I'd rather be the super-personally-identifying FirstNameLastname or the rather-too-casual CartesianDaemon. Neither have ever been registered before me yet, although I've seen some CartesianDemons with the more usual spelling.