Cutting in a circle/ellipse (PIL)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • flaerpen
    New Member
    • Jul 2007
    • 5

    Cutting in a circle/ellipse (PIL)

    Hello, I wonder how to 'cut' or erase some parts of a image/circle that i've done in PIL.

    Let say that I have two circles that intersects with eachother, I know the two points where they intersect and now I want to cut/erase the circle thats left.

    Let me show you a image how I want it to look like, that I made in paint:



    I really hope you understand and if not, tell me! /flaerpen
  • flaerpen
    New Member
    • Jul 2007
    • 5

    #2
    I have a possible answer. Instead of writing a ellipse/circle you write an Chord(?). but how do I use my points with the chord-function in PIL?

    from http://www.pythonware.com/library/pi.../imagedraw.htm
    chord

    draw.chord(xy, start, end, options)

    Same as arc, but connects the end points with a straight line.

    The outline option gives the colour to use for the chord outline. The fill option gives the colour to use for the chord interior.
    EDIT: Or maybe this is not the solution, because if there is more than one intersection with another circle how do you do that?

    Comment

    • flaerpen
      New Member
      • Jul 2007
      • 5

      #3
      I now know (i think) that i'll use the chord-function, but have to ask how to calculate the start and end angle in a general formula.

      I made a picture so it can be easier to show you what I really want:


      Is there anyone who can help me to create a general forumula for the two green lines?

      /flaerpen

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        Originally posted by flaerpen
        I now know (i think) that i'll use the chord-function, but have to ask how to calculate the start and end angle in a general formula.

        I made a picture so it can be easier to show you what I really want:


        Is there anyone who can help me to create a general forumula for the two green lines?

        /flaerpen
        flaerpen - This class will calculate the angles in radians with respect to circle 1 center point and the circle intersection points:[code=Python]class CircleCircleInt ersection(objec t):
        def __init__(self, p1, p2, r1, r2):
        '''
        Given circle center points p1 and p2 and circle radii r1 and r2
        Calculate intersection points of circles
        This only works in the X-Y plane at the present time
        '''
        self.p1 = p1
        self.p2 = p2
        self.r1 = r1
        self.r2 = r2
        self.d = p1.dist(p2)
        if self.d > r1+r2:
        self.Pa = None
        self.Pb = None
        elif self.d < abs(r1-r2):
        self.Pa = None
        self.Pb = None
        else:
        self.a = (r1**2-r2**2+self.d**2 )/(2*self.d)
        self.b = self.d-self.a
        self.P0 = polarPt(p1, p2, self.a, p1)
        self.h = (r1**2-self.a**2)**0.5
        self.intPts()

        def intPts(self):
        self.Pa = Point()
        self.Pb = Point()
        self.Pa.x = self.P0.x + (self.h * (self.p2.y - self.p1.y) / self.d)
        self.Pb.x = self.P0.x - (self.h * (self.p2.y - self.p1.y) / self.d)
        self.Pa.y = self.P0.y - (self.h * (self.p2.x - self.p1.x) / self.d)
        self.Pb.y = self.P0.y + (self.h * (self.p2.x - self.p1.x) / self.d)
        self.theta0 = math.atan2(self .p2.y+self.p1.y , self.p2.x+self. p1.x)
        self.theta1 = self.theta0 - math.atan2(self .h, self.a)
        self.theta2 = self.theta0 + math.atan2(self .h, self.a)[/code]Here is function 'polarPt':[code=Python]def polarPt (p1, p2, d, p3=False):
        '''
        Calculate the vector p1-->p2
        Translate distance 'd' parallel to vector p1-->p2 from point 'p3'
        Return the new point
        'p3' defaults to 'p2'
        >>> polarPt(p1,p2,1 0,p3)
        Point(4.651484, 24.128709, 8.825742)
        >>> polarPt(p1,p2,1 0)
        Point(10.651484 , 19.128709, 7.825742)
        >>>
        '''
        return (p3 or p2)+(p2-p1).uv()*d[/code]You will need a point object with 'x' and 'y' attributes, a unit vector (uv()) method, a distance (dist()) method, and + - * overloads. The angles calculated are counter-clockwise.[code=Python]>>> a = CircleCircleInt ersection(Point (), Point(-2,5.5,0), 3.5, 3.0)
        >>> a.theta0
        1.9195673303788 037
        >>> a.theta1
        1.5052297601267 781
        >>> a.theta2
        2.3339049006308 294
        >>> a.h
        1.4090427459286 017
        >>> a.a
        3.2038412164378 536
        >>> a.P0
        Point(-1.094891, 3.010949, 0.000000)
        >>> a.Pa
        Point(0.229319, 3.492479, 0.000000)
        >>> a.Pb
        Point(-2.419100, 2.529418, 0.000000)
        >>> [/code]I used Paul Bourke's website for reference. LINK

        Comment

        Working...