# Is There Package or Method to Calculate Certain Area in Julia (symbolically with SymPy)?

**URL:** https://discourse.julialang.org/t/is-there-package-or-method-to-calculate-certain-area-in-julia-symbolically-with-sympy/99751
**Category:** General Usage
**Tags:** question
**Created:** [June 2, 2023, 3:57am UTC](https://discourse.julialang.org/t/is-there-package-or-method-to-calculate-certain-area-in-julia-symbolically-with-sympy/99751 "2023-06-02T03:57:57Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)
#### Post date: [June 2, 2023, 3:57am UTC](https://discourse.julialang.org/t/is-there-package-or-method-to-calculate-certain-area-in-julia-symbolically-with-sympy/99751/1 "2023-06-02T03:57:57Z")

</div>

Hi all,

there are two circles, one is bigger than the other, and overlapping a bit, I want to know could Julia help to calculate the bigger circle shaded area? Maybe this is geometry problem. But it is on Calculus book.

This is the solution:

 ![Capture d’écran_2023-06-02_10-52-26](https://global.discourse-cdn.com/julialang/original/3X/8/8/885b78f09d1022297cd89423cb01de3f1d3777e1.png)

I am using Latex to draw it, I can’t hardly plot it with Julia, too difficult:

```julia
\documentclass[12pt]{article}
\usepackage{tikz}
\usetikzlibrary{decorations.pathreplacing}
\usetikzlibrary {calc}

\newcommand{\MarkRightAngle}[4][.3cm]% #1=size (optional), #2-#4 three points: \angle #2#3#4
{\coordinate (tempa) at ($(#3)!#1!(#2)$);
	\coordinate (tempb) at ($(#3)!#1!(#4)$);
	\coordinate (tempc) at ($(tempa)!0.5!(tempb)$);%midpoint
	\draw (tempa) -- ($(#3)!2!(tempc)$) -- (tempb);
}

\begin{document}
	\begin{tikzpicture}[scale=1,>=latex,x=1cm,y=0.8cm]
		
		\draw[fill=blue!7] (0,0) ellipse (4 and 4);% a circle 
		\draw[fill=blue!7] (-4,0) arc (180:360:4 and 4);% left half of the circle starting at (-4,0) from 180 to 360
		\draw[fill=white!7] (0,-3) ellipse (3 and 3);% a circle 
		
		\draw (0,0.2) node {$A$};
		\draw (0,-3.6) node {$C$};
		\draw (-3.5,-3.1) node {$D$};	
		\draw (3.5,-3.1) node {$B$};
		\draw (1.5,-3.2) node {$a$};	
		
		% Create right angle
		\coordinate (A) at (0,0);
		\coordinate (E) at (1.6,-1.4);
		\coordinate (C) at (0,-3);
		\MarkRightAngle{A}{E}{C}
		
		\draw[dashed] (0,-3) -- (1.6,-1.4);
		\draw[-,thick] (0,0) -- (-3,-2.7) node[left, midway] {\footnotesize $b$};
		\draw[-,thick] (0,0) -- (3,-2.7) node[right, midway] {\footnotesize $E$};
		\draw[-,thick] (0,0) -- (0,-3) node[left, midway] {\footnotesize $a$};
		\draw[-,thick] (-3,-2.7) -- (0,-3);
		\draw[-,thick] (3,-2.7) -- (0,-3);
		
	\end{tikzpicture}
\end{document}

```

The question is:  
**If I plot it with Julia, two circles with certain radius and center, can I compute the certain area that is not intersected with other geometry object like the solution above?**

---

<div class="post-metadata">

### Author: ![DanielVandH](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielvandh/32/31134_2.png) [@DanielVandH](https://discourse.julialang.org/u/DanielVandH)
#### Post date: [June 2, 2023, 4:13am UTC](https://discourse.julialang.org/t/is-there-package-or-method-to-calculate-certain-area-in-julia-symbolically-with-sympy/99751/2 "2023-06-02T04:13:13Z")

</div>

Never used it, but you could take a look at [GitHub - mancellin/PolygonArea.jl: A claimless Julia library to compute the area of unions and intersections of polygons](https://github.com/mancellin/PolygonArea.jl). I don’t imagine you’d be able to do much here symbolically, though.

---

<div class="post-metadata">

### Author: ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)
#### Post date: [June 2, 2023, 4:20am UTC](https://discourse.julialang.org/t/is-there-package-or-method-to-calculate-certain-area-in-julia-symbolically-with-sympy/99751/3 "2023-06-02T04:20:02Z")

</div>

Alright thanks for this, I will check it out

---

<div class="post-metadata">

### Author: ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)
#### Post date: [June 2, 2023, 7:12am UTC](https://discourse.julialang.org/t/is-there-package-or-method-to-calculate-certain-area-in-julia-symbolically-with-sympy/99751/4 "2023-06-02T07:12:47Z")

</div>

The package can draw intersection and union, but not setminus…

```julia
using PolygonArea, Plots

r = rectangle((0.0, 0.0), (1.0, 1.0))

c1 = circle((0.9, 1.5), 1.5, 100) # Actually, a regular 100-gon
c2 = circle((0.9, 3), 2, 100) # Actually, a regular 100-gon

cinter = c1 ∩ c2
#a = area(c1 ∩ c2)
#println("area = ", a)
plot(c2)
plot!(c1, color=:green)

#plot(c1 ∖ c2) does not work

```

---

<div class="post-metadata">

### Author: ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)
#### Post date: [June 3, 2023, 3:28pm UTC](https://discourse.julialang.org/t/is-there-package-or-method-to-calculate-certain-area-in-julia-symbolically-with-sympy/99751/5 "2023-06-03T15:28:06Z")

</div>

To draw the figure you could use this package, although perhaps it needs to be tuned better for a better definition.  
To have a symbolic solution, either do it by hand or try with GPT v5

```julia
using ImplicitEquations, Plots
f(x,y) = (x-0.9)^2+(y-1.5)^2-1.5^2
g(x,y) = (x-0.9)^2+(y-3.)^2-2.0^2

plot((g ≦ 0) & (f ≫ 0) , fc=:black, widen=false, aspect_ratio=:equal)   

```

this is the figure obtained. Maybe someone knows how to make it better.

 ![moon](https://global.discourse-cdn.com/julialang/original/3X/2/7/27b67c90966362cd71ec1160a57e47be9328b7c7.jpeg)

---

<div class="post-metadata">

### Author: ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)
#### Post date: [June 3, 2023, 9:32pm UTC](https://discourse.julialang.org/t/is-there-package-or-method-to-calculate-certain-area-in-julia-symbolically-with-sympy/99751/6 "2023-06-03T21:32:56Z")

</div>

i tried kinda PLAYING with sympy.

```julia
using SymPy

@syms x y

julia> f(x,y) = (x-9//10)^2+(y-3//2)^2-(3//2)^2
f (generic function with 2 methods)

julia> g(x,y) = (x-9//10)^2+(y-3)^2-2^2
g (generic function with 1 method)

julia> ((x1,y1),(x2,y2))=solve([f(x,y) , g(x,y)], x, y)
2-element Vector{Tuple{Sym, Sym}}:
 (9/10 - 2*sqrt(5)/3, 5/3)
 (9/10 + 2*sqrt(5)/3, 5/3)

julia> ft(x,y) = (x)^2+(y+5//3-3//2)^2-(3//2)^2
ft (generic function with 1 method)

julia> gt(x,y) = (x)^2+(y+5//3-3)^2-2^2
gt (generic function with 1 method)

julia> ((x1,y1),(x2,y2))=solve([ft(x,y) , gt(x,y)], x, y)
2-element Vector{Tuple{Sym, Sym}}:
 (-2*sqrt(5)/3, 0)
 (2*sqrt(5)/3, 0)

julia> nscf(x),pscf(x)=solve(ft(x,y),y)
2-element Vector{Sym}:
 -sqrt(9 - 4*x^2)/2 - 1/6
  sqrt(9 - 4*x^2)/2 - 1/6

julia> S1=integrate(pscf(x), x1,x2)
             ⎛4⋅√5⎞
       9⋅asin⎜────⎟
  √5 ⎝ 9 ⎠
- ── + ────────────
  9 4

julia> nscg(x),pscg(x)=solve(gt(x,y),y)
2-element Vector{Sym}:
 4/3 - sqrt(4 - x^2)
 sqrt(4 - x^2) + 4/3

julia> S2=integrate(nscg(x), x1,x2)
        ⎛√5⎞ 8⋅√5
- 4⋅asin⎜──⎟ + ────
        ⎝3 ⎠ 9

julia> Sym(π)*4- (S1-S2)
                     ⎛4⋅√5⎞
               9⋅asin⎜────⎟
        ⎛√5⎞ ⎝ 9 ⎠
- 4⋅asin⎜──⎟ - ──────────── + √5 + 4⋅π
        ⎝3 ⎠ 4

```

---

<div class="post-metadata">

### Author: ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)
#### Post date: [June 6, 2023, 8:32am UTC](https://discourse.julialang.org/t/is-there-package-or-method-to-calculate-certain-area-in-julia-symbolically-with-sympy/99751/7 "2023-06-06T08:32:29Z")

</div>

Very beautiful @rocco_sprmnt21 . This is why I find enjoyment learning Mathematics and sharing here in this discourse. The experts help beginner like me a lot.

---

<div class="post-metadata">

### Author: ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)
#### Post date: [June 6, 2023, 2:56pm UTC](https://discourse.julialang.org/t/is-there-package-or-method-to-calculate-certain-area-in-julia-symbolically-with-sympy/99751/8 "2023-06-06T14:56:44Z")

</div>

I too (not being a real expert, like many others in this forum) find it interesting to follow the discussions and, when possible, participate.

I would like to add an observation. Maybe a bit fussy, but that might be why symbolic solving with SymPy functions doesn’t come “easy”.

The practical observation is that the result shown in the book is valid only for a/b \>=1/2.

For a/b\<1/2 the gray surface is always S(a/b=1/2)

An additional observation is that it would be nice to be able to add assumptions (constraints?) in order to “oblige” the SymPy functions to give only the “physically/geometrically” expected solutions.

---

<div class="post-metadata">

### Author: ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)
#### Post date: [June 6, 2023, 5:36pm UTC](https://discourse.julialang.org/t/is-there-package-or-method-to-calculate-certain-area-in-julia-symbolically-with-sympy/99751/9 "2023-06-06T17:36:10Z")

</div>

Without losing generality, the figures can be translated into a reference of Cartesian axes, so as to have the center of the circle of radius b at the origin O of the axes.  
Similarly, to make life easier for SymPy, you can scale everything by a factor b, so that b=\>1 to a=\>a/b.  
So the transformations that serve to symbolically solve the problem are the following.

```julia

julia> using SymPy

julia> @syms x
(x,)

julia> @vars a y positive=true
(a, y)

julia> ft(x,y) = (x-a)^2+(y)^2-a^2
ft (generic function with 1 method)

julia> gt(x,y) = (x)^2+(y)^2-1
gt (generic function with 1 method)

julia> (x1,y1)=solve([ft(x,y) , gt(x,y)], x, y)[1]
(1/(2*a), sqrt(2*a - 1)*sqrt(2*a + 1)/(2*a))

julia> nscf(x),pscf(x)=solve(ft(x,y),y)
2-element Vector{Sym}:
 -sqrt(x*(2*a - x))
  sqrt(x*(2*a - x))

julia> S1=integrate(expand(pscf(x)), (x, 0,x1)) # using pscf(x) whitout expand give a messy formula
       ⎛ 1 ⎞
       ⎜-a + ───⎟
 2 ⎜ 2⋅a⎟
a ⋅asin⎜────────⎟ 2 __________
       ⎝ a ⎠ π⋅a ╱ 1 ⎛ a 1 ⎞
───────────────── + ──── + ╱ 1 - ──── ⋅⎜- ─ + ───⎟
        2 4 ╱ 2 ⎝ 2 4⋅a⎠
                           ╲╱ 4⋅a

julia> nscg(x),pscg(x)=solve(gt(x,y),y)
2-element Vector{Sym}:
 -sqrt(1 - x^2)
  sqrt(1 - x^2)

julia> S2=integrate(pscg(x), (x, x1,1))
                       __________
                      ╱ 1
      ⎛ 1 ⎞ ╱ 1 - ────
  asin⎜───⎟ ╱ 2
      ⎝2⋅a⎠ π ╲╱ 4⋅a
- ───────── + ─ - ───────────────
      2 4 4⋅a

```

I enclose the analogous operations done by hand.  
With a little patience it can be verified that the two results are equivalent

 ![by hand](https://global.discourse-cdn.com/julialang/original/3X/e/0/e02e1ee6f6a51d29fb0880ff70c4e6ad53648d21.jpeg)

---

<div class="post-metadata">

### Author: ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)
#### Post date: [June 7, 2023, 9:43am UTC](https://discourse.julialang.org/t/is-there-package-or-method-to-calculate-certain-area-in-julia-symbolically-with-sympy/99751/10 "2023-06-07T09:43:34Z")

</div>

That is a very nice observation knowing the result only valid for a/b \>= 1/2.

We will always need assumptions and constraints then, because if not limited then the solution is boundless.

---

<div class="post-metadata">

### Author: ![mancellin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mancellin/32/13971_2.png) [@mancellin](https://discourse.julialang.org/u/mancellin)
#### Post date: [June 7, 2023, 11:55am UTC](https://discourse.julialang.org/t/is-there-package-or-method-to-calculate-certain-area-in-julia-symbolically-with-sympy/99751/11 "2023-06-07T11:55:34Z")

</div>

Hi, I’m the author of PolygonArea.jl. Thank you for considering using it!

Actually set difference has been implemented but with the backslash `\` operator as I wasn’t aware of `\setminus` (sorry for the lack of documentation).

Anyway, the implementation of PolygonArea is dreadfully inefficient for concave shapes with many edges, such as your `c1 \ c2`.  
But if you only need the area, maybe you could do instead

```julia
area(c1) - area(c1 ∩ c2)

```

that should work fine.

---

<div class="post-metadata">

### Author: ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)
#### Post date: [June 8, 2023, 5:26am UTC](https://discourse.julialang.org/t/is-there-package-or-method-to-calculate-certain-area-in-julia-symbolically-with-sympy/99751/12 "2023-06-08T05:26:48Z")

</div>

Hi @mancellin ,

thanks for the package, it is really amazing and useful for students like me.

I want to straighten out that the mistake is on the character, I type this:

```julia
plot(c1 ∖ c2)

```

but the backlash is from copy paste from Julia Documentation, I should have type backlash directly from keyboard, notice this when I see it from Mousepad / Notepad of Linux:  
 ![Capture d’écran_2023-06-08_12-22-12](https://global.discourse-cdn.com/julialang/original/3X/f/c/fc4d4800a168f4656717ab01b66424db6d0e6bed.png)

The above won’t work. but below will work.

My mistake.

This code works:

```julia
# https://discourse.julialang.org/t/is-there-package-or-method-to-calculate-certain-area-in-julia-symbolically-with-sympy/99751/4

using PolygonArea, Plots

r = rectangle((0.0, 0.0), (1.0, 1.0))

c1 = circle((0.9, 1.5), 1.5, 100) # Actually, a regular 100-gon
c2 = circle((0.9, 3), 2, 100) # Actually, a regular 100-gon

cinter = c1 ∩ c2
#a = area(c1 ∩ c2)
#println("area = ", a)
#plot(c2)
#plot!(c1, color=:green)

plot(c2 \ c1) 

```

![Capture d’écran_2023-06-08_12-25-53](https://global.discourse-cdn.com/julialang/original/3X/4/9/4991ffbc3e1404498170c0cfc1bbcb93b09a0a60.png)

I love trying Julia and its packages, it is amazing for people around the world to learn Mathematics with Computer Science at one time.

---

<div class="post-metadata">

### Author: ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)
#### Post date: [June 9, 2023, 3:39am UTC](https://discourse.julialang.org/t/is-there-package-or-method-to-calculate-certain-area-in-julia-symbolically-with-sympy/99751/13 "2023-06-09T03:39:39Z")

</div>

This is something that I want to ask:

 ![Capture d’écran_2023-06-09_10-36-22](https://global.discourse-cdn.com/julialang/original/3X/2/1/2143d200249ccfcf93560ef0c36d4a7554ef2a03.png)

If we can get the \theta and \phi from angle DCB and angle DAB in terms of \arccos and \arcsin from the solution above, **Why the solution suggest that \theta and \phi are in radians?**

Btw,

b = radius of big circle  
a = radius of small circle

The small circle perimeter (North perimeter) hit the center of the big circle.

---

<div class="post-metadata">

### Author: ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)
#### Post date: [June 9, 2023, 6:43am UTC](https://discourse.julialang.org/t/is-there-package-or-method-to-calculate-certain-area-in-julia-symbolically-with-sympy/99751/14 "2023-06-09T06:43:02Z")

</div>

I don’t know how pertinent this is to Julia and I’m not sure of the author’s motivation in preferring the use of radians to degrees.  
I would think that a good reason could be the greater simplicity of the formulas, in the case of the use of radians.  
For example a famous limit of analysis `[lim(sin(x)/x) when x->0] = 1` only if x is measured in radians.
