Ejemplo n.º 1
0
class LineTests(unittest.TestCase, ShapeTests):
    def setUp(self):
        self.init_kwargs = {}
        self.init_kwargs["xCoord"] = 50
        self.init_kwargs["yCoord"] = 50
        self.init_kwargs["xCoord2"] = 50
        self.init_kwargs["yCoord2"] = 50
        self.init_kwargs["measurements"] = {}
        self.area = 0
        self.shape = Line(**self.init_kwargs)

    def test_compute_area(self):
        area = self.shape.compute_area()
        self.assertEqual(0, self.area)

    def test_move(self):
        move_kwargs = {}
        move_kwargs["newXCoord"] = 2
        move_kwargs["newYCoord"] = 2
        move_kwargs["newXCoord2"] = 4
        move_kwargs["newYCoord2"] = 4
        self.shape.move(**move_kwargs)
        self.assertEqual(
            self.shape.get_coords(),
            [
                (move_kwargs["newXCoord"], move_kwargs["newYCoord"]),
                (move_kwargs["newXCoord2"], move_kwargs["newYCoord2"]),
            ],
        )

    def test_scale(self):
        self.scale_setUp()
        self.assertEqual(self.before, self.after)
        self.assertEqual(0, self.after)
Ejemplo n.º 2
0
 def setUp(self):
     self.init_kwargs = {}
     self.init_kwargs["xCoord"] = 50
     self.init_kwargs["yCoord"] = 50
     self.init_kwargs["xCoord2"] = 50
     self.init_kwargs["yCoord2"] = 50
     self.init_kwargs["measurements"] = {}
     self.area = 0
     self.shape = Line(**self.init_kwargs)
Ejemplo n.º 3
0
    def __init__(self, type, axis, x0, y0, slope):
        self._type = type

        if   axis == 'X' or axis == 'x' or axis == 0:
            self._axis = 0
        elif axis == 'Y' or axis == 'y' or axis == 1:
            self._axis = 1
        elif axis == 'Z' or axis == 'z' or axis == 2:
            self._axis = 2
        else:
            print axis
            "Unkown axis !!"
            stop
        Line.__init__(self, x0, y0, slope)
        return
Ejemplo n.º 4
0
    def addLine(self,
                pointA,
                pointB,
                lw=2,
                color='k',
                ls='solid',
                clip=True,
                add=True,
                mplprops={}):
        """
			addLine - Adds a line 'shape' to the Figures.
			Args:
				pointA (List[float]): List or tuple, in x,y, of the first point.
				pointB (List[float]): List or tuple, in x,y, of the second point.
				lw (Optional[float]): Line width of the line. Default is 2.
				color (Optional[str]): Color of the line. Default is 'k'
				mplprops (Optional[dict]): Dictionary to pass directly to the matplotlib object. Default is {}.
			Returns:
				Line.Line object
		"""

        l = Line.Line(self.cust_float(pointA), self.cust_float(pointB), lw, ls,
                      color, clip, mplprops, self)
        if add: self.drawOrder.append(l)
        return l
Ejemplo n.º 5
0
 def mousePressEvent(self, event):   # отслеживание кликов по мыши и добавление фигуры в список
     if self.instrument == 'brush':
         self.objects.append([])
         self.objects[-1].append(BrushPoint(event.x(), event.y(), self.thickness, self.color))
         self.update()
     elif self.instrument == 'line':
         self.objects.append(Line(event.x(), event.y(), event.x(), event.y(), self.thickness, self.color))
         self.update()
     elif self.instrument == 'circle':
         self.objects.append(Circle(event.x(), event.y(), event.x(), event.y(), self.thickness, self.color))
         self.update()
     elif self.instrument == 'rubber':
         self.objects.append([])
         self.objects[-1].append(BrushPoint(event.x(), event.y(), self.thickness, QColor(240, 240, 240)))
         self.update()
     elif self.instrument == 'rect':
         self.objects.append(Rect(event.x(), event.y(), 0, 0, self.thickness, self.color))
         self.update()
     elif self.instrument == 'square':
         self.objects.append(Rect(event.x(), event.y(), 0, 0, self.thickness, self.color))
         self.update()
     elif self.instrument == 'triangle':
         self.objects.append(Triangle(event.x(), event.y(), event.x(), event.y(), self.thickness, self.color))
         self.update()
     elif self.instrument == 'sqtriangle':
         self.objects.append(SqTriangle(event.x(), event.y(), event.x(), event.y(), self.thickness, self.color))
         self.update()
Ejemplo n.º 6
0
 def __init__(self):
     super().__init__()
     self.model.add_obj(Line("noia", np.array([10, 10, 1]), np.array([100, 100, 1])))
     self.model.add_obj(rect("wee", np.array([10, 10, 1]), np.array([80, 80, 0])))
     self.model.add_obj(Point("kkk", 69, 69))
     self.model.add_obj(Bezier("weed", np.array([69, 89, 1]), np.array([99, 109, 1]), np.array([269, 190, 1]), np.array([270, 290, 1])))
     self.win_main: MainController = self.win_main
Ejemplo n.º 7
0
 def on_btn_add_line_clicked(self, sender: Gtk.Button) -> None:
     name = self.name_line.get_text()
     x1 = float(self.entry_x1_line.get_text())
     y1 = float(self.entry_y1_line.get_text())
     z1 = float(self.entry_z1_line.get_text())
     x2 = float(self.entry_x2_line.get_text())
     y2 = float(self.entry_y2_line.get_text())
     z2 = float(self.entry_z2_line.get_text())
     self.model.add_obj(Line(name, pt(x1, y1, z1), pt(x2, y2, z2)))
     self.win.hide()
Ejemplo n.º 8
0
 def build_object(self, obj):
     name = obj["name"]
     vertices = obj["points"]
     n = len(vertices)
     if n == 1:
         x, y, z, *_ = vertices[0].array
         return Point(name, x, y, z)
     elif n == 2:
         return Line(name, *(v.array[:3] for v in vertices))
     else:
         return Polygon(name, *(v.array[:3] for v in vertices))
Ejemplo n.º 9
0
 def clip_line(self, line: Line) -> tp.Optional[Line]:
     p1, p2 = line._ppc
     d1, d2 = self.direction_of(p1), self.direction_of(p2)
     if not (d1 | d2):
         # completely inside the window
         return line
     elif d1 & d2:
         # completely outside the window
         return None
     new_p1 = self.snap(d1, p2 - p1, p1)
     new_p2 = self.snap(d2, p1 - p2, p2)
     line._ppc = np.vstack((new_p1, new_p2))
     return line
Ejemplo n.º 10
0
    def draw_ring(self):
        """Function that draws a line on the Orbit following the Planet"""

        for i in range(1, self.__revolution_loop):
            x1 = self.orbit_list[i].x
            y1 = self.orbit_list[i].y
            x2 = self.orbit_list[i - 1].x
            y2 = self.orbit_list[i - 1].y
            Line(
                x1,
                y1,
                0,
                x2,
                y2,
                0,
            ).draw()
Ejemplo n.º 11
0
    def draw_ringAll(self):
        """Function that draws a line on the Orbit"""

        for i in range(1, len(self.orbit_list)):
            x1 = self.orbit_list[i].x
            y1 = self.orbit_list[i].y
            x2 = self.orbit_list[i - 1].x
            y2 = self.orbit_list[i - 1].y
            Line(
                x1,
                y1,
                0,
                x2,
                y2,
                0,
            ).draw()
Ejemplo n.º 12
0
 def draw(self, shape):
     # Method drawing a shape on the canvas.
     self.__canvas.delete(self.__tmp_rendered)
     rendered_shape = None
     if shape['type'] == 'rectangle' and shape['mode'] == 'normal':
         rendered_shape = Rectangle(shape)
     elif shape['type'] == 'rectangle' and shape['mode'] == 'straight':
         rendered_shape = Square(shape)
     elif shape['type'] == 'oval' and shape['mode'] == 'normal':
         rendered_shape = Oval(shape)
     elif shape['type'] == 'oval' and shape['mode'] == 'straight':
         rendered_shape = Circle(shape)
     elif shape['type'] == 'line' and shape['mode'] == 'normal':
         rendered_shape = Line(shape)
     elif shape['type'] == 'line' and shape['mode'] == 'straight':
         rendered_shape = Diagonal(shape)
     shape_id = rendered_shape.draw_on(self.__canvas)
     return shape_id
Ejemplo n.º 13
0
    def draw(self):
        '''Draws all objects that are in the gameobjects list to the screen'''
        self.screen.fill((0, 0, 0))
        yaxis = Line(WHITE, 5)
        yaxis.draw(self.screen, [
            Vector2(0, -(SCREEN_HEIGHT / 2)),
            Vector2(0, (SCREEN_HEIGHT / 2))
        ])
        xaxis = Line(WHITE, 5)
        xaxis.draw(
            self.screen,
            [Vector2(-(SCREEN_WIDTH / 2), 0),
             Vector2(SCREEN_WIDTH / 2, 0)])
        mxpos, mypos = pygame.mouse.get_pos()
        mouse = Rectangle(WHITE, Vector2(25, 25))
        mpos = worldtoscreen(Vector2(mxpos, mypos))
        mpos = mpos.scale(-1)
        mouse.draw(self.screen, mpos)

        for gameobject in self.gameobjects:
            gameobject.draw(self.screen)
Ejemplo n.º 14
0
def on_mouse_release(x, y, button, modifiers):
    global lines
    cur.point2 = Point2D(x,y)
    lines += [Line(cur.point1.copy(),cur.point2.copy())]
    pass
Ejemplo n.º 15
0
 def draw_line(self, event):
     l = Line(self.c)
     self.c.bind('<Button-1>', l.shape_create_start)
     self.c.bind('<B1-Motion>', l.shape_create)
Ejemplo n.º 16
0
 def addLine(self, t):
     self.processedroot.append(
         Line(t.get('x1'), t.get('y1'), t.get('x2'), t.get('y2')))
Ejemplo n.º 17
0
#  This program tests several of the geometric shape classes.
#

from ezgraphics import GraphicsWindow
from shapes import Rectangle, Line

# Create the window.
win = GraphicsWindow()
canvas = win.canvas()

# Draw a rectangle.
rect = Rectangle(10, 10, 90, 60)
rect.setFill("light yellow")
rect.draw(canvas)

# Draw another rectangle.
rect.moveBy(rect.getWidth(), rect.getHeight())
rect.draw(canvas)

# Draw six lines of different colors.
colors = ["red", "green", "blue", "yellow", "magenta", "cyan"]

line = Line(10, 150, 300, 150)

for i in range(6):
    line.setColor(colors[i])
    line.draw(canvas)
    line.moveBy(10, 40)

win.wait()
Ejemplo n.º 18
0
import pyglet
from pyglet.gl import glClearColor, glClear, GL_COLOR_BUFFER_BIT
from point import Point2D
from shapes import Line

window = pyglet.window.Window(width=1000,height=700,resizable=True)

ticks = 120

cur = Line(Point2D(0,0),Point2D(0,0),drawable=True)
lines = []
started = False

@window.event
def on_draw():
    global started
    glClearColor(1, 0.9, 0.8, 0)
    glClear(GL_COLOR_BUFFER_BIT)
    if not started:
        return
    cur.draw()
    for line in lines:
        line.draw()


@window.event
def on_mouse_press(x, y, button, modifiers):
    global started
    started = True
    cur.point1 = Point2D(x,y)
    cur.point2 = Point2D(x,y)
Ejemplo n.º 19
0
    def build_grid(self):

        intervals = Intervals()
        intervals.interval_type = self.interval_type
        intervals.x = self.x
        intervals.start = self.start
        intervals.finish = self.finish
        intervals.resolution = self.resolution
        intervals.week_start = self.week_start

        line = Line()
        line.y = self.y
        line.dy = self.y + self.height
        line.stroke_color = self.line_color
        line.stroke_width = self.line_width
        line.stroke_dasharray = self.line_dashing

        lines = str()

        for i in intervals.get_intervals():
            line.x = i[0]
            line.dx = line.x
            lines += line.svg

        last_line = self.x + ((self.finish - self.start) * self.resolution)
        line.x = last_line
        line.dx = last_line
        lines += line.svg

        return lines
Ejemplo n.º 20
0
FLAG_HEIGHT = FLAG_WIDTH * 2 // 3
PART_WIDTH = FLAG_WIDTH // 3

# Create the graphics window.
win = GraphicsWindow(300, 300)
canvas = win.canvas()

# Build the flag as a group shape.
flag = Group()

part = Rectangle(0, 0, PART_WIDTH, FLAG_HEIGHT)
part.setColor("green")
flag.add(part)

part = Rectangle(PART_WIDTH * 2, 0, PART_WIDTH, FLAG_HEIGHT)
part.setColor("red")
flag.add(part)

flag.add(Line(PART_WIDTH, 0, PART_WIDTH * 2, 0))
flag.add(Line(PART_WIDTH, FLAG_HEIGHT, PART_WIDTH * 2, FLAG_HEIGHT))

# Draw the first flag in the upper-left area of the canvas.
flag.moveBy(10, 10)
flag.draw(canvas)

# Draw the second flag in the bottom-right area of the canvas.
flag.moveBy(130, 180)
flag.draw(canvas)

win.wait()
Ejemplo n.º 21
0
    def calculate_shapes(self):
        """returns a list of shapes for plotting"""
        shapes = []

        # calculate the range of x, y
        if self.curves:
            x_max = max([max(curve['x']) for curve in self.curves])
            x_min = min([min(curve['x']) for curve in self.curves])
            y_max = max([max(curve['y']) for curve in self.curves])
            y_min = min([min(curve['y']) for curve in self.curves])
            self.x_range = (x_min, x_max)
            self.y_range = (y_min, y_max)

        # calculate axis
        y_axis_start = (self.margin + 10, self.margin)
        y_axis_end = (y_axis_start[0],
                      y_axis_start[1] + self.height - 2 * self.margin)
        y_axis = Line(self.axis_color, y_axis_start, y_axis_end)
        shapes.append(y_axis)

        x_axis_start = y_axis_end
        x_axis_end = (x_axis_start[0] + self.width - 2 * self.margin,
                      x_axis_start[1])
        x_axis = Line(self.axis_color, x_axis_start, x_axis_end)
        shapes.append(x_axis)

        # values on the axis
        anchor_length = 2
        anchors = 5
        anchor_distance = int((y_axis_end[1] - y_axis_start[1]) / 5)
        mid_point = (0, 0)
        for i in range(anchors):
            start_pos = (y_axis_start[0],
                         y_axis_start[1] + anchor_distance * (i + 1))
            end_pos = (start_pos[0] - anchor_length, start_pos[1])
            anchor_shape = Line(self.axis_color, start_pos, end_pos)
            shapes.append(anchor_shape)

            if i == 0:
                mid_point = start_pos

            # add value text for the anchor
            if self.y_range[1] - self.y_range[0]:
                anchor_value = int(self.y_range[1] * 0.25 * (anchors - 1 - i))

                rendered_text = self.digit_font.render(str(anchor_value), True,
                                                       self.text_color)
                anchor_text = Text(rendered_text,
                                   (end_pos[0] - 17, end_pos[1] - 3))
                shapes.append(anchor_text)
            elif i == 0:
                anchor_value = int(self.y_range[1])

                rendered_text = self.digit_font.render(str(anchor_value), True,
                                                       self.text_color)
                anchor_text = Text(rendered_text,
                                   (end_pos[0] - 17, end_pos[1] - 3))
                shapes.append(anchor_text)

        # add points and constants
        last_x, last_y = (0, 0)
        for curve in self.curves:
            x_values = curve['x']
            y_values = curve['y']
            for i in range(len(x_values)):
                if not self.x_range[1] - self.x_range[0]:
                    x, y = mid_point
                else:
                    try:
                        x = int(x_axis_start[0] +
                                (x_axis_end[0] - x_axis_start[0] - 20) /
                                (self.x_range[1] - 1) * (x_values[i] - 1))
                    except ZeroDivisionError:
                        x = mid_point[0]
                    try:
                        y = int(y_axis_end[1] - anchor_distance *
                                (anchors - 1) / self.y_range[1] * y_values[i])
                    except ZeroDivisionError:
                        y = mid_point[1]

                # connect points with line
                if i:
                    line = Line(curve['color'], (last_x, last_y), (x, y))
                    shapes.append(line)

                last_x, last_y = x, y

            # add label to curve
            if curve['label']:
                label_text = self.label_font.render(curve['label'], True,
                                                    curve['color'])
                label = Text(label_text, (last_x + 5, last_y - 3))
                shapes.append(label)

        if self.constants:
            for constant in self.constants:
                start_x = x_axis_start[0]
                end_x = x_axis_end[0] - 20
                if self.y_range[1] - self.y_range[0] and constant[
                        'value'] - self.y_range[0]:
                    y = int(y_axis_end[1] - anchor_distance * (anchors - 1) /
                            self.y_range[1] * constant['value'])
                else:
                    y = mid_point[1]
                line = DashLine(constant['color'], (start_x, y), (end_x, y),
                                dash_length=5)
                shapes.append(line)

                # add label to constant
                if constant['label']:
                    constant_text = self.label_font.render(
                        constant['label'], True, constant['color'])
                    constant = Text(constant_text, (end_x + 5, y - 3))
                    shapes.append(constant)

        # add caption
        if self.caption:
            rendered_text = self.caption_font.render(self.caption, True,
                                                     self.text_color)
            caption_text = Text(rendered_text, (self.margin + 40, 0))
            shapes.append(caption_text)

        self._add_offset(shapes)

        return shapes