473,394 Members | 2,090 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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
Jul 4 '07 #1
3 3014
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?
Jul 4 '07 #2
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
Jul 9 '07 #3
bvdet
2,851 Expert Mod 2GB
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:
Expand|Select|Wrap|Line Numbers
  1. class CircleCircleIntersection(object):
  2.     def __init__(self, p1, p2, r1, r2):
  3.         '''
  4.         Given circle center points p1 and p2 and circle radii r1 and r2
  5.         Calculate intersection points of circles
  6.         This only works in the X-Y plane at the present time
  7.         '''
  8.         self.p1 = p1
  9.         self.p2 = p2
  10.         self.r1 = r1
  11.         self.r2 = r2
  12.         self.d = p1.dist(p2)
  13.         if self.d > r1+r2:
  14.             self.Pa = None
  15.             self.Pb = None
  16.         elif self.d < abs(r1-r2):
  17.             self.Pa = None
  18.             self.Pb = None
  19.         else:
  20.             self.a = (r1**2-r2**2+self.d**2)/(2*self.d)
  21.             self.b = self.d-self.a
  22.             self.P0 = polarPt(p1, p2, self.a, p1)
  23.             self.h = (r1**2-self.a**2)**0.5
  24.             self.intPts()
  25.  
  26.     def intPts(self):
  27.         self.Pa = Point()
  28.         self.Pb = Point()
  29.         self.Pa.x = self.P0.x + (self.h * (self.p2.y - self.p1.y) / self.d)
  30.         self.Pb.x = self.P0.x - (self.h * (self.p2.y - self.p1.y) / self.d)
  31.         self.Pa.y = self.P0.y - (self.h * (self.p2.x - self.p1.x) / self.d)
  32.         self.Pb.y = self.P0.y + (self.h * (self.p2.x - self.p1.x) / self.d)
  33.         self.theta0 = math.atan2(self.p2.y+self.p1.y, self.p2.x+self.p1.x)
  34.         self.theta1 = self.theta0 - math.atan2(self.h, self.a)
  35.         self.theta2 = self.theta0 + math.atan2(self.h, self.a)
Here is function 'polarPt':
Expand|Select|Wrap|Line Numbers
  1. def polarPt (p1, p2, d, p3=False):
  2.     '''
  3.     Calculate the vector p1-->p2
  4.     Translate distance 'd' parallel to vector p1-->p2 from point 'p3'
  5.     Return the new point
  6.     'p3' defaults to 'p2'
  7.     >>> polarPt(p1,p2,10,p3)
  8.     Point(4.651484, 24.128709, 8.825742)
  9.     >>> polarPt(p1,p2,10)
  10.     Point(10.651484, 19.128709, 7.825742)
  11.     >>> 
  12.     '''
  13.     return (p3 or p2)+(p2-p1).uv()*d
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.
Expand|Select|Wrap|Line Numbers
  1. >>> a = CircleCircleIntersection(Point(), Point(-2,5.5,0), 3.5, 3.0)
  2. >>> a.theta0
  3. 1.9195673303788037
  4. >>> a.theta1
  5. 1.5052297601267781
  6. >>> a.theta2
  7. 2.3339049006308294
  8. >>> a.h
  9. 1.4090427459286017
  10. >>> a.a
  11. 3.2038412164378536
  12. >>> a.P0
  13. Point(-1.094891, 3.010949, 0.000000)
  14. >>> a.Pa
  15. Point(0.229319, 3.492479, 0.000000)
  16. >>> a.Pb
  17. Point(-2.419100, 2.529418, 0.000000)
  18. >>> 
I used Paul Bourke's website for reference. LINK
Jul 9 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Brian Basquille | last post by:
I have an image drawn on my form using GDI+ like so: // image of puck is loaded and drawn Image imagePuck = Image.FromFile("Puck.png"); g.DrawImage(imagePuck, puck_x, puck_y, 36, 36); The...
20
by: chump1708 | last post by:
Can anyone give me an idea for drawing a circle without making use of any floating point computations? Thanks Prasad
4
by: Matt Haggard | last post by:
I'm using PIL (Python Imaging Library) to generate button images. They consist of a left end image, a middle, repeating image and a right side image.... anyway, that's not important I'm using a...
11
by: Christopher Ireland | last post by:
Hello! Using the definition for en ellipse (http://en.wikipedia.org/wiki/Ellipse) I can draw an arc of points. However, the end points of this arc do not coincide with the end points of an arc...
3
by: surbhi bhargava | last post by:
hello i m a student of bioinformatics. i am having problem in C /C++ coding in computer graphics. i want to know the code for drawing a line,circle,ellipse in computer graphics using C language...
0
by: Tem | last post by:
I need to draw a black circle using WPF and generate an image file. I used the following but it resulted in a blank file. I cannot figure out what is wrong with it. Thanks, Tem ...
4
by: Slickuser | last post by:
How do I automatic move the X & Y to create a ellipse function as below, DrawCircle? Or there any sample source code out there? I want it move down by user input. How can I achieve this?...
1
by: PankajGaur | last post by:
Hi, I need to create a Ellipse shaped user control which have folloiwng attributes: 1) The edges are smooth 2) The control shuld only be selected when click on the ellipse shape & not when...
0
by: =?Utf-8?B?UGFua2FqR2F1cg==?= | last post by:
Hi, I need to create a Ellipse shaped user control which have folloiwng attributes: 1) The edges are smooth 2) The control shuld only be selected when click on the ellipse shape & not when...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.