示例#1
0
    def _extract_point_and_angle(self, view, name, pose_type):
        # Get the "new" and "old" values of the reference point.
        new_view_point = view.variables[name]
        gui_attrs = getattr(new_view_point, 'gui', None)
        if gui_attrs is not None and gui_attrs.old_value is not None:
            old_view_point = gui_attrs.old_value
        else:
            old_view_point = None

        view_point = new_view_point
        if pose_type == INITIAL_POSE and old_view_point:
            # Look at the old values of the reference point.
            view_point = old_view_point

        angle = None
        if isinstance(view_point, Tri):
            if pose_type == FINAL_POSE and old_view_point:
                dp_old = old_view_point.ip - old_view_point.p
                tri_was_just_created = (dp_old.norm() == 0.0)
                if tri_was_just_created:
                    # This Tri reference point was just created. We ignore its
                    # value so that apply_backward() can set the initial
                    # rotation angle. The user will have a chance of changing
                    # the reference point and the associated angle in future
                    # drag actions.
                    return (Point(view_point.p), angle)
            dp = view_point.ip - view_point.p
            view_point = view_point.p
            if dp.norm2() > 0.0:
                angle = dp.angle()
        return (Point(view_point), angle)
示例#2
0
    def _calc_refpoints(self, bone_matrix, point3, angle):
        moved_view_points = []
        for view, point_name in self.dst_view_points:
            mx = np.dot(view.view_matrix, bone_matrix)
            new_value = Point(*np.dot(mx, point3)[:2])
            old_value = view.variables[point_name]
            if isinstance(old_value, Tri) and old_value.ip is not None:
                # If we are dealing with a Tri, then new_value must be a
                # Tri as well.
                r = (old_value.p - old_value.ip).norm()
                p = new_value
                new_value = Tri(p + r*Point.vangle(angle), p)

            view.variables[point_name] = new_value
            moved_view_points.append((view, point_name))
        return moved_view_points
示例#3
0
 def __init__(self,
              size=Point(50.0, 8.0),
              completed=0.0,
              bg_color=None,
              fg_color=None):
     self.size = size
     self.completed = completed
     self.bg_color = bg_color or Color.grey
     self.fg_color = fg_color or Color.blue
示例#4
0
 def draw(self, w):
     lx, ly = self.size
     px = lx * min(1.0, max(0.0, self.completed))
     ps = [Point(0, 0), Point(lx, 0), Point(lx, ly), Point(0, ly)]
     g = Gradient(Line(ps[0], ps[1]), self.fg_color, Color.white,
                  self.fg_color)
     w << Poly(self.bg_color, *ps)
     w << Poly(ps[0], Point(px, 0), Point(px, ly), ps[3], g)
     w << Poly(Style(Color.none, Border(Color.black, 0.2)), *ps)
示例#5
0
文件: fractree.py 项目: mfnch/pyrtist
#!PYRTIST:VERSION:0:0:1
from pyrtist.lib2d import Point, Tri
#!PYRTIST:REFPOINTS:BEGIN
bbox1 = Point(-33.3736933815, 87.8159051329)
bbox2 = Point(76.1030045728, -1.65034200668)
gui1 = Point(34.5696427817, 2.57241672105)
#!PYRTIST:REFPOINTS:END
from pyrtist.lib2d import *
from pyrtist.lib2d.tools import Bar

# We define a new object, which contains all the details about
# the Pythagorean tree
def draw_tree(dest, level, p1, p2, xmid, h, d=1e-10):
    g = 1.0 / (level + 1)
    b = 1 - g
    c = Color(0.5*b, g, 0)

    side = Line(p1, p2)
    p = Poly(side[0], side[1 - d], side[1 - d, 1], side[0, 1], c) >> dest
    t = Line(p[2], p[3], p[2 + xmid, -h])

    if level >= 1:
        draw_tree(dest, level - 1, t[2], t[0], xmid, h, d)
        draw_tree(dest, level - 1, t[1], t[2], xmid, h, d)

w = Window()
w << BBox(bbox1, bbox2)

# Now we can draw the tree with the following line
gui_r = Bar(cursor=gui1, interval=(0, 15), label='level={:.0f}') >> w
示例#6
0
#!PYRTIST:VERSION:0:0:1
from pyrtist.lib2d import Point, Tri
#!PYRTIST:REFPOINTS:BEGIN
bbox1 = Point(0.0, 47.4489709172); bbox2 = Point(91.3037539683, -1.781098434)
p1 = Point(10.2040816327, 38.6904453846)
p2 = Point(26.1904761905, 39.2006490635)
p3 = Point(38.2653061224, 38.6904453846)
p4 = Point(55.4421768707, 36.6496306689)
p5 = Point(65.4761904762, 33.7584764883)
p6 = Point(70.0680272109, 44.6428216388)
p7 = Point(83.843537415, 43.9625500669)
p8 = Point(82.6530612245, 36.4795627759)
p9 = Point(76.3605442177, 29.8469149498)
p10 = Point(2.21088435374, 26.7856928763)
p11 = Point(15.8163265306, 28.486371806)
p12 = Point(22.1088435374, 18.7925019064)
p13 = Point(6.46258503401, 10.2891072575)
q1 = Point(32.8231292517, 29.6768470569)
q2 = Point(26.0204081633, 14.370736689)
q3 = Point(43.1972789116, 16.2414835117)
q4 = Point(54.9319727891, 14.8809403679)
q5 = Point(45.9183673469, 23.7244708027)
q6 = Point(57.9931972789, 24.0646065886)
q7 = Point(48.8095238095, 16.2414835117)
q8 = Point(61.9047619048, 12.3299219732)
q9 = Point(66.156462585, 25.5952176254)
q10 = Point(72.2789115646, 16.2414835117)
q11 = Point(78.0612244898, 24.0646065886)
q12 = Point(77.8911564626, 12.4999898662)
q13 = Point(88.3503401361, 18.7074925426)
q14 = Point(12.8867030612, 4.80486655518)
示例#7
0
#!PYRTIST:VERSION:0:0:1
from pyrtist.lib2d import Point, Tri
#!PYRTIST:REFPOINTS:BEGIN
bbox1 = Point(0.0, 50.0)
bbox2 = Point(100.0, 0.0)
p1 = Point(13.2653061224, 15.5612119398)
p2 = Point(23.8095238095, 38.6904453846)
p3 = Point(38.7755102041, 18.2822982274)
p4 = Point(41.156462585, 7.22788518395)
p5 = Point(75.0, 39.2006490635)
p6 = Point(88.7755102041, 16.5816192977)
p7 = Point(66.156462585, 19.3027055853)
#!PYRTIST:REFPOINTS:END
from pyrtist.lib2d import *

w = Window()
w << BBox(bbox1, bbox2)

# The line below defines a Poly object, representing a polygon.
# The new object is passed directly to the Window object which
# just draws it.
# p1, p2, p3 are three ``reference'' points which are shown
# in the GUI as small white squares. They can be selected
# (they become yellow) with a left mouse click (keep the SHIFT
# key pressed to select multiple points). Once a point is yellow
# it can be moved with a drag-and-drop action.
w << Poly(p1, p2, p3)

# The line below creates a new 'Poly' object and - rather
# than passing it to the Window - assigns it to a new variable.
# No drawing is performed at this stage.
示例#8
0
#!PYRTIST:VERSION:0:0:1
from pyrtist.lib2d import Point, Tri
#!PYRTIST:REFPOINTS:BEGIN
bbox1 = Point(-22.6152212121, 21.856384472)
bbox2 = Point(22.357695082, -21.4080237288)
p1 = Point(-0.255807878788, -19.5900623377)
p2 = Point(19.7791411043, -0.0303101075269)
p3 = Point(0.127607361963, 19.7838873118)
p4 = Point(-20.5447852761, 0.223718064516)
#!PYRTIST:REFPOINTS:END
from pyrtist.lib2d import *

from pyrtist.lib2d.prefabs.electric import *

# This is not a real Wheatstone bridge.
# We replace some of the resistors with other components, just to make
# the example more interesting!
w = Window()

d1 = 20.0  # d1 = half diagonal
w << BBox(bbox1, bbox2)

# Places the four components.
# Each component has two connections. Here we want to rotate and translate
# ("rt") a resistor such that the first of its terminals is near to
# the point p1 and the second one is near to p2.
r12 = Put(capacitor, "rt", Near(0, p1), Near(1, p2)) >> w
# We proceed in a similar way for the other components:
r23 = Put(diode, "rt", Near("A", p2), Near("K", p3)) >> w
r34 = Put(inductance, "rt", Near(0, p3), Near(1, p4)) >> w
r41 = Put(resistor, "rt", Near(0, p4), Near(1, p1)) >> w
示例#9
0
#!PYRTIST:VERSION:0:0:1
from pyrtist.lib2d import Point, Tri
#!PYRTIST:REFPOINTS:BEGIN
bbox1 = Point(-1.0, 1.0); bbox2 = Point(1.0, -1.0); center1 = Point(0.0, 0.0)
p1 = Point(0.1381917585406386, 0.329695651405736)
#!PYRTIST:REFPOINTS:END
from pyrtist.opengl import *

w = FragmentWindow(bbox1, bbox2, position_name='frag_pos')

w << '''
in vec2 frag_pos;

out vec4 out_color;

vec2 tx(vec2 p) {
  return (p - bbox2) / (bbox1 - bbox2);
}

void main() {
  vec2 p = tx(frag_pos);

  vec4 col = vec4(p, 0.0, 1.0);

  if (length(p - tx(p1)) < 0.1)
    col += vec4(0.0, 0.0, 0.5, 0.0);

  if (min(p.x, p.y) < 0.0 || max(p.x, p.y) > 1.0)
    col = vec4(0.0, 0.0, 0.0, 1.0);

  out_color = col;
示例#10
0
#!PYRTIST:VERSION:0:0:1
from pyrtist.lib2d import Point, Tri
#!PYRTIST:REFPOINTS:BEGIN
bbox1 = Point(0.0, 50.0)
bbox2 = Point(100.0, 12.5838926174)
p1 = Point(3.15540458874, 46.942241204)
p2 = Point(3.23537580547, 42.1395946309)
p4 = Point(28.5119375629, 38.1285583893)
q1 = Point(73.1545885714, 21.8120805369)
q3 = Point(93.6244457143, 38.4228187919)
q5 = Point(66.4133738602, 33.8755592617)
q6 = Point(94.2249240122, 24.9089847651)
q7 = Point(84.8024316109, 26.7326948322)
q2 = Tri(q5, Point(70.1344457143, 37.2483221477))
q4 = Tri(q6, Point(90.4365171429, 20.1342281879), q7)
#!PYRTIST:REFPOINTS:END
# TUTORIAL EXAMPLE N.3
# How to create a closed figure using bezier curves.

from pyrtist.lib2d import *

w = Window()

s = "             "
font = Font(2, "Helvetica")
w << Args(
    Text(
        p1, font, Offset(0, 1), Color.red,
        "Pyrtist allows creating curves (cubic Bezier splines)."
        "\nThis example explains how."),
    Text(
示例#11
0
#!PYRTIST:VERSION:0:0:1
from pyrtist.lib2d import Point, Tri
#!PYRTIST:REFPOINTS:BEGIN

#!PYRTIST:REFPOINTS:END
from pyrtist.lib2d import *

w = Window()

# Define a linear and radial gradient.
g1 = Gradient(Line((-25, -25), (0, 0)), Color.red, Color.yellow)
g2 = Gradient(Circles(Point(10, 5), 0, 12), Color.white, Color.black)

ball = Window(Circle(g2, Point(10, 5), 12))

w << Args(
    Poly(g1, (-25, -25), (25, -25), (25, 25), (-25, 25)),
    Put(ball),  # Just place the ball, as it is.
    Put(ball, Point(-15, -10), Scale(0.2, 0.5),
        AngleDeg(45))  # Scale and rotate it.
)

gui(w)

#w.save("gradient.png")
示例#12
0
文件: fractal.py 项目: mfnch/pyrtist
#!PYRTIST:VERSION:0:0:1
from pyrtist.lib2d import Point, Tri
#!PYRTIST:REFPOINTS:BEGIN
bbox1 = Point(-23.0957515662, 79.6991129258)
bbox2 = Point(71.3423454797, -1.88531784342)
p1 = Point(-20.2154615182, 30.6496131137)
p2 = Point(-2.9523785489, 65.0652930926)
p3 = Point(35.303325298, 77.1040972766)
p4 = Point(69.2944192672, 43.4906900368)
p5 = Point(25.8613547511, 9.49960406392)
v1 = Point(52.0608243954, 2.61783485327)
#!PYRTIST:REFPOINTS:END
from pyrtist.lib2d import *
from pyrtist.lib2d.tools import Bar

w = Window()
w << BBox(bbox1, bbox2)


def draw_fractal(dest, points, color, d, n, level=0):
    poly = Poly(color, *points) >> dest
    new_points = [poly[i + d] for i, p in enumerate(poly)]
    new_color = color.darken(1.0 - 5.0 / n)
    if level > 0:
        draw_fractal(dest, new_points, new_color, d, n, level - 1)


bar_r = Bar(cursor=v1, interval=(10, 300), label='level={:.0f}') >> w
n = int(bar_r.value)

w << BBox(bbox1, bbox2)
示例#13
0
#!PYRTIST:VERSION:0:0:1
from pyrtist.lib2d import Point, Tri
#!PYRTIST:REFPOINTS:BEGIN
gui1 = Point(19.4886046743, 3.84443340587)
gui2 = Point(-3.56565488372, 9.39682833333)
gui3 = Point(53.0555553922, -4.96428689655)
#!PYRTIST:REFPOINTS:END
from pyrtist.lib2d import *


class Bar(object):
    def __init__(self,
                 size=Point(50.0, 8.0),
                 completed=0.0,
                 bg_color=None,
                 fg_color=None):
        self.size = size
        self.completed = completed
        self.bg_color = bg_color or Color.grey
        self.fg_color = fg_color or Color.blue

    def draw(self, w):
        lx, ly = self.size
        px = lx * min(1.0, max(0.0, self.completed))
        ps = [Point(0, 0), Point(lx, 0), Point(lx, ly), Point(0, ly)]
        g = Gradient(Line(ps[0], ps[1]), self.fg_color, Color.white,
                     self.fg_color)
        w << Poly(self.bg_color, *ps)
        w << Poly(ps[0], Point(px, 0), Point(px, ly), ps[3], g)
        w << Poly(Style(Color.none, Border(Color.black, 0.2)), *ps)
示例#14
0
#!PYRTIST:VERSION:0:0:1
from pyrtist.lib2d import Point, Tri
#!PYRTIST:REFPOINTS:BEGIN
bbox1 = Point(0, 50); bbox2 = Point(100, 0)
p1 = Point(25.7575757576, 30.3787878788)
p2 = Point(19.814314022, 27.0345941837)
p3 = Point(27.7318108704, 19.2297513487)
p4 = Point(47.8787878788, 18.4090909091)
p5 = Point(67.7272727273, 24.7727272727)
p6 = Point(49.3939393939, 36.2878787879)
p7 = Point(28.4848484848, 38.8636363636); p8 = Point(40.4545454545, 42.5)
p9 = Point(38.9393939394, 35.9848484848)
#!PYRTIST:REFPOINTS:END
# Example showing how to draw arcs and compute their bounding box.
from pyrtist.lib2d import *

w = Window()
w << BBox(bbox1, bbox2)

# Define three arcs.
arc1 = Arc(Through(p1, p2, p3), Close.no)
arc2 = Arc(Through(p4, p5, p6), Color.red, StrokeStyle(1, Color.blue, Cap.round))
arc3 = Arc(Through(p7, p8, p9))

# Draw them.
w << Stroke(StrokeStyle(0.5), Path(arc1)) << arc2 << arc3

# Find the bounding box of arc3 and draw it as a rectangle.
bb = BBox(arc3)
w << Stroke(0.2, Color.red, Rectangle(*bb))
示例#15
0
#!PYRTIST:VERSION:0:0:1
from pyrtist.lib2d import Point, Tri
#!PYRTIST:REFPOINTS:BEGIN
bbox1 = Point(-1.78114006536, 62.385)
bbox2 = Point(76.8434514839, -2.13)
#!PYRTIST:REFPOINTS:END
# Here we show how to draw a small electric circuit.

from pyrtist.lib2d import *
from pyrtist.lib2d.prefabs.electric import *

# These parameters control the spacing of the circuit.
sx, sy = (15.0, 30.0)  # scales along x and y directions.
# x offsets and y offsets.
x0, x1, x2, x3, x4 = [sx * x for x in (0.0, 1.0, 2.0, 3.5, 4.5)]
y0, y1, y2 = [sy * y for y in (0.0, 1.0, 2.0)]

w = Window()

# Place the four resistors, the two condenser and the two NPN transistors
# The string "rt" means: automatically rotate and translate such that
# the Near() constraints are better satisfied.
r1 = Put(resistor, "rt", Near(0, (x1, y0)), Near(1, (x1, y1))) >> w
r2 = Put(resistor, "rt", Near(0, (x2, y0)), Near(1, (x2, y1))) >> w
r3 = Put(resistor, "rt", Near(0, (x3, y0)), Near(1, (x3, y1))) >> w
r4 = Put(resistor, "rt", Near(0, (x4, y0)), Near(1, (x4, y1))) >> w
c1 = Put(capacitor, "rt", Near(0, (x1, y1)), Near(1, (x2, y1))) >> w
c2 = Put(capacitor, "rt", Near(0, (x4, y1)), Near(1, (x3, y1))) >> w
t1 = Put(transistor_NPN, "rt", Near("C", (x1, y1)), Near("E", (x1, y2))) >> w
t2 = Put(transistor_NPN, "rt", Scale(1, -1), Near("C", (x4, y1)),
         Near("E", (x4, y2))) >> w
示例#16
0
文件: house.py 项目: mfnch/pyrtist
#!PYRTIST:VERSION:0:0:1
from pyrtist.lib2d import Point, Tri
#!PYRTIST:REFPOINTS:BEGIN
bbox1 = Point(15.6225412606, 30.7384856532)
bbox2 = Point(38.0423256326, 11.8849630531)
bbox3 = Point(-13.8721863036, 71.6629154857)
bbox4 = Point(86.7246167767, -10.7160646028)
p1 = Point(17.4669426557, 38.4105960265)
p2 = Point(16.6067117105, 28.0125458067)
p3 = Point(19.6192672026, 21.8148990154)
p4 = Point(35.5133561647, 22.5165562914)
p5 = Point(49.4206840066, 22.5165562914)
p6 = Point(56.2087845008, 24.5033112583)
p7 = Point(55.8776576474, 32.119205298)
p8 = Point(54.5531502339, 40.2317880795)
p9 = Point(41.8047663789, 39.4039735099)
p10 = Point(25.5795505634, 39.4039735099)
p11 = Point(36.159656, 29.967347351)
p12 = Point(16.977676112, 10.6486261589)
p13 = Point(57.4495143196, 11.0927152318)
p14 = Point(12.4519598622, 22.5657303129)
p15 = Point(11.5978402783, 40.1604624732)
p16 = Point(26.0608908833, 40.0465806148)
p17 = Point(26.3455941817, 22.7080815671)
p18 = Point(27.7910894389, 22.2810307033)
p19 = Point(27.4874049505, 20.2311572519)
p20 = Point(11.8475618262, 20.0033935351)
p21 = Point(11.3920350935, 21.8634638891)
p22 = Point(13.9354631185, 24.3308841546)
p23 = Point(13.5938162539, 38.1105890219)
p24 = Point(24.1848690591, 38.2244708803)
示例#17
0
#!PYRTIST:VERSION:0:0:1
from pyrtist.lib2d import Point, Tri
#!PYRTIST:REFPOINTS:BEGIN
bbox1 = Point(0, 50)
bbox2 = Point(100, 0)
p1 = Point(45.0, 13.2575757576)
p2 = Point(29.8484848485, 39.1666666667)
p3 = Point(70.3030303031, 27.4999999999)
p4 = Point(34.5827690377, 31.0710407031)
p5 = Point(78.8827259507, 22.3629947834)
#!PYRTIST:REFPOINTS:END
from pyrtist.lib2d import *

w = Window()
w << BBox(bbox1, bbox2)

w << Line(p1, p2, 0.3)
radius = 10.0
w << Circle(p3, radius, Style(Color.none, Border(0.3, Color.black)))

# Project p4 onto the line.
v = (p2 - p1).normalized()
p4 = p1 + v * ((p4 - p1).dot(v))

# Project p5 onto the circle.
p5 = p3 + radius * (p5 - p3).normalized()

gui.move(p4=p4, p5=p5)
gui(w)
示例#18
0
#!PYRTIST:VERSION:0:0:1
from pyrtist.lib2d import Point, Tri
#!PYRTIST:REFPOINTS:BEGIN

#!PYRTIST:REFPOINTS:END
# One example taken from the samples page of the Cairo graphic library
# (http://cairographics.org/samples/), rewritten for Pyrtist.

from pyrtist.lib2d import *

w = Window()

pat1 = Gradient(Line((0, 256), (0, 0)), Color.white, Color.black)
pat2 = Gradient(Circles(Point(115.2, 153.6), 25.6, Point(102.4, 153.6), 128.0),
                Color.white, Color.black)
w << Poly((0, 0), (256, 0), (256, 256), (0, 256), pat1)
w << Circle((128, 128), 76.8, pat2)

#w.save("cairo_gradient.png", mode="rgb24", resolution=1)

gui(w)
示例#19
0
#!PYRTIST:VERSION:0:0:1
from pyrtist.lib2d import Point, Tri
#!PYRTIST:REFPOINTS:BEGIN
bbox1 = Point(30.5430530612, 47.9487259669)
bbox2 = Point(91.596572, 2.55635137931)
gradient_1 = Point(80.7540587224, 20.1550310997)
gradient_2 = Point(85.6947292383, 35.1338943299)
gui1 = Point(62.9776352697, 13.1407282123)
gui2 = Point(65.2669775934, 3.96126117318)
gui3 = Point(89.4384517766, 30.4841370558)
gui4 = Point(89.4384517766, 37.8813451777)
gui5 = Point(73.1541878173, 35.5856598985)
gui6 = Point(34.9967846473, 16.4555357542)
gui7 = Point(33.7249278008, 24.3600768156)
gui8 = Point(41.6034263959, 43.7480964467)
gui9 = Point(52.417302799, 46.9387755102)
normal_1 = Point(51.8444233207, 32.5715189873)
normal_2 = Point(45.9996831432, 39.95)
ruler_1 = Point(49.2415813278, 22.0652100559)
ruler_2 = Point(61.4514070539, 25.3800175978)
ruler_3 = Point(68.1080481622, 34.6069620253)
rulerx_1 = Point(45.934753527, 25.6350027933)
rulerx_2 = Point(55.6008655602, 21.5552396648)
rulery_1 = Point(58.0112896806, 22.3508853952)
rulery_2 = Point(62.1677267813, 30.2716455326)
rulerz_1 = Point(61.5009505703, 31.5537974684)
rulerz_2 = Point(59.9762357414, 35.8791139241)
v1 = Point(55.597715736, 25.1275380711)
v2 = Point(56.8699238579, 30.2290609137)
v3 = Point(59.7221166033, 33.0803797468)
v4 = Point(54.8343908629, 34.5653553299)