示例#1
0
    def draw(self, Canvas, line):
        c_w = 10 / Canvas.Scale
        c_h_long = 10 /Canvas.Scale
        c_h_short = 3 / Canvas.Scale
        point = self.pos
        leftPoint = (point[0]-c_w, point[1])
        rightPoint = (point[0]+c_w, point[1])



        def rightCompressor(l_point, r_point):
            upLeftPoint = (l_point[0], l_point[1]+c_h_long)
            downLeftPoint = (l_point[0], l_point[1]-c_h_long)
            upRightPoint = (r_point[0], r_point[1]+c_h_short)
            downRightPoint = (r_point[0], r_point[1]-c_h_short)
            return [upLeftPoint, downLeftPoint, downRightPoint, upRightPoint]

        def leftCompressor(l_point, r_point):
            upLeftPoint = (l_point[0], l_point[1]+c_h_short)
            downLeftPoint = (l_point[0], l_point[1]-c_h_short)
            upRightPoint = (r_point[0], r_point[1]+c_h_long)
            downRightPoint = (r_point[0], r_point[1]- c_h_long)
            return [upLeftPoint, downLeftPoint, downRightPoint, upRightPoint]


        ## find the change in x on the line to determine the
        ## direction of the edge
        delta_x = Geom.delta_x(*line)

        ## get the angle that the line makes with the x-axis
        theta = Geom.angleFromXaxis(line)

        ## if it's negative the direction in the x is right justified
        if delta_x < 0:
            ## so we construct a right justified shape
            compressor = rightCompressor(leftPoint, rightPoint)

        ## if it's positive the direction in the x is left justified
        elif delta_x > 0:
            ## so we construct a left justified shape
            compressor = leftCompressor(leftPoint, rightPoint)

        ## Uh Oh Vertical lines, now we have to think about the change in y
        elif delta_x == 0:
            ## the change in y
            delta_y = Geom.delta_y(*line)

            ## if it's positive then the direction in the y is up
            if delta_y < 0:
                ## since we will ultimately be rotating 90 degrees
                ## for a vertical line pointing up
                ## we want the shape to be right justified
                ## (think about it)
                compressor = rightCompressor(leftPoint, rightPoint)

            ##if it's negative then the direction in the y is down
            elif delta_y > 0:
                ## and we construct a left justified shape
                compressor = leftCompressor(leftPoint, rightPoint)

        ## if the delta y is also zero then our line is two of
        ## the same point and we have some bad input
        ## since this code is just for prototyping and fast development
        ## we will make due with a descriptive warning message printed
        ## to the console, and we'll make compressor right justified
        ## don't worry if this happens it will be obvious
        else:
            compressor = rightCompressor(leftPoint, rightPoint)


        ## and then we rotate it acordingly
        compressor = Geom.rotatePointList(compressor, theta, point)

        ## finally we draw this darned thing to the Canvas
        Canvas.AddPolygon(
            compressor,
            LineWidth=1,
            LineColor="BLUE",
            FillColor="BLUE",
        )