def _lineTo(self, pt): if self.filterDoubles: if pt == self.currentPt: return hits = splitLine(self.currentPt, pt, self.value, self.isHorizontal) if len(hits) > 1: # result will be 2 tuples of 2 coordinates # first two points: start to intersect # second two points: intersect to end # so, second point in first tuple is the intersect # then, the first coordinate of that point is the x. if self.contourIndex not in self.hits: self.hits[self.contourIndex] = [] if self.isHorizontal: self.hits[self.contourIndex].append(round(hits[0][-1][0], 4)) else: self.hits[self.contourIndex].append(round(hits[0][-1][1], 4)) if self.isHorizontal and pt[1] == self.value: # it could happen if self.contourIndex not in self.hits: self.hits[self.contourIndex] = [] self.hits[self.contourIndex].append(pt[0]) elif (not self.isHorizontal) and (pt[0] == self.value): # it could happen if self.contourIndex not in self.hits: self.hits[self.contourIndex] = [] self.hits[self.contourIndex].append(pt[1]) self.currentPt = pt
def append_point_coordinate_line(contour, rpoints, where, is_horizontal): """ Appends RPoint object to line(RSegment object) by horizontal or vertical line. Args: contour:: RContour The RContour object that you want to add RPoint object. rpoints:: list A list of RPoint objects. It should be containing startpoint and endpoint of line. The order of start and end follows a index of contour.points. where:: int or float The coordinate value of line(x value if uses vertical line otherwise y value). is_horizontal:: bool If this is True, uses horizontal line(coordinate value of y). Otherwise uses vertical line(coordinate value of x). Raises: arguments value error:: ValueError If not found target segment in contour, raises this error. This can be occured when the rpoints(RPoint objects) are not in the contour.points. splitting error:: AssertionError If function of splitting is not done properly, raises this error. For example, if it split one line(or curve) but result is also one line(or curve). """ points = _r2t(rpoints) new_line = splitLine(points[0], points[1], where, is_horizontal) assert (len(new_line) > 1) segment_index = _segment_index_of(contour, rpoints[1]) if segment_index is None: raise ValueError('Not found target segment in contour.') contour.insertSegment(segment_index, 'line', [new_line[0][1]]) contour.round()
def test_splitLine(): assert splitLine((0, 0), (100, 100), where=50, isHorizontal=True) == [((0, 0), (50.0, 50.0)), ((50.0, 50.0), (100, 100))] assert splitLine((0, 0), (100, 100), where=100, isHorizontal=True) == [((0, 0), (100, 100))] assert splitLine((0, 0), (100, 100), where=0, isHorizontal=True) == [((0, 0), (0, 0)), ((0, 0), (100, 100))] assert splitLine((0, 0), (100, 100), where=0, isHorizontal=False) == [((0, 0), (0, 0)), ((0, 0), (100, 100))] assert splitLine((100, 0), (0, 0), where=50, isHorizontal=False) == [((100, 0), (50, 0)), ((50, 0), (0, 0))] assert splitLine((0, 100), (0, 0), where=50, isHorizontal=True) == [((0, 100), (0, 50)), ((0, 50), (0, 0))] assert splitLine((0, 100), (100, 100), where=50, isHorizontal=True) == [((0, 100), (100, 100))]
def test_splitLine(): assert splitLine( (0, 0), (100, 100), where=50, isHorizontal=True ) == [((0, 0), (50.0, 50.0)), ((50.0, 50.0), (100, 100))] assert splitLine( (0, 0), (100, 100), where=100, isHorizontal=True ) == [((0, 0), (100, 100))] assert splitLine( (0, 0), (100, 100), where=0, isHorizontal=True ) == [((0, 0), (0, 0)), ((0, 0), (100, 100))] assert splitLine( (0, 0), (100, 100), where=0, isHorizontal=False ) == [((0, 0), (0, 0)), ((0, 0), (100, 100))] assert splitLine( (100, 0), (0, 0), where=50, isHorizontal=False ) == [((100, 0), (50, 0)), ((50, 0), (0, 0))] assert splitLine( (0, 100), (0, 0), where=50, isHorizontal=True ) == [((0, 100), (0, 50)), ((0, 50), (0, 0))] assert splitLine( (0, 100), (100, 100), where=50, isHorizontal=True ) == [((0, 100), (100, 100))]