-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathcircle.go
276 lines (237 loc) · 10.6 KB
/
circle.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package gogeom
import (
"fmt"
"math"
)
// (x-a)*2 + (y-b)*2 = r*2
// (x-c)*2 + (y-d)*2 = r*2
type RadiusFormOfCircle struct {
A, B, R0, C, D, R1 float64
}
// x2 + y2 + Ax + By+ C = 0
// x2 + y2 + Dx + Ey + F = 0
type GeneralFormOfCircle struct {
A, B, C, D, E, F float64
}
// DistanceBetweenTwoCenters is used to calculate the distance between two circles
func (c *RadiusFormOfCircle) DistanceBetweenTwoCenters() float64 {
distance := math.Sqrt(PowerFunction(c.A-c.C, 2) + PowerFunction(c.B-c.D, 2))
return distance
}
// Does Circle Intersect tells you if both the circle intersect or not
func (c *RadiusFormOfCircle) DoesCircleIntersect() bool {
isInterescting := false
if (c.R0 - c.R1) > 0 {
if c.DistanceBetweenTwoCenters() > (c.R0-c.R1) && c.R0+c.R1 > c.DistanceBetweenTwoCenters() {
isInterescting = true
}
} else if (c.R0 - c.R1) < 0 {
if c.DistanceBetweenTwoCenters() > -(c.R0-c.R1) && c.R0+c.R1 > c.DistanceBetweenTwoCenters() {
isInterescting = true
}
}
return isInterescting
}
//Area of circle with Radius R0 and R1
func (c *RadiusFormOfCircle) AreaOfCircles() (float64, float64) {
areaOne := math.Pi * PowerFunction(c.R0, 2)
areaTwo := math.Pi * PowerFunction(c.R1, 2)
return areaOne, areaTwo
}
//circumference of circle with Radius R0 and R1
func (c *RadiusFormOfCircle) CircumferenceOfCircles() (float64, float64) {
circumferenceOne := math.Pi * c.R0 * 2
circumferenceTwo := math.Pi * c.R1 * 2
return circumferenceOne, circumferenceTwo
}
//Calculates the minimum delta
// ∂ is the area of the triangle formed by the two circle centers and one of the intersection point.
// The sides of this triangle are S, r0 and R0 , the area is calculated by Heron' s formula.
func (c *RadiusFormOfCircle) CalculateDelta() float64 {
delta := math.Sqrt((c.DistanceBetweenTwoCenters()+c.R0+c.R1)*(c.DistanceBetweenTwoCenters()+c.R0-c.R1)*(c.DistanceBetweenTwoCenters()-c.R0+c.R1)*(-c.DistanceBetweenTwoCenters()+c.R0+c.R1)) / 4
return delta
}
//Calculates A,B,C,D
// (x-a)*2 + (y-b)*2 = r*2 is one equation
//(x-c)*2 + (y-d)*2 = r*2 is second equation
func (c *RadiusFormOfCircle) CalculateXY() (float64, float64, float64, float64) {
distance := c.DistanceBetweenTwoCenters()
delta := c.CalculateDelta()
A := ((c.A + c.C) / 2) + ((c.C - c.A) * (PowerFunction(c.R0, 2) - PowerFunction(c.R1, 2)) / (2 * PowerFunction(distance, 2))) + (2 * ((c.B - c.D) / PowerFunction(distance, 2)) * delta)
C := ((c.A + c.C) / 2) + ((c.C - c.A) * (PowerFunction(c.R0, 2) - PowerFunction(c.R1, 2)) / (2 * PowerFunction(distance, 2))) - (2 * ((c.B - c.D) / PowerFunction(distance, 2)) * delta)
B := ((c.B + c.D) / 2) + ((c.D - c.B) * (PowerFunction(c.R0, 2) - PowerFunction(c.R1, 2)) / (2 * PowerFunction(distance, 2))) - (2 * ((c.A - c.C) / PowerFunction(distance, 2)) * delta)
D := ((c.B + c.D) / 2) + ((c.D - c.B) * (PowerFunction(c.R0, 2) - PowerFunction(c.R1, 2)) / (2 * PowerFunction(distance, 2))) + (2 * ((c.A - c.C) / PowerFunction(distance, 2)) * delta)
return A, B, C, D
}
//Calculates A,B
// (x-a)*2 + (y-b)*2 = r*2 is one equation
func (c *RadiusFormOfCircle) CalculateAB() (float64, float64) {
distance := c.DistanceBetweenTwoCenters()
delta := c.CalculateDelta()
A := ((c.A + c.C) / 2) + ((c.C - c.A) * (PowerFunction(c.R0, 2) - PowerFunction(c.R1, 2)) / (2 * PowerFunction(distance, 2))) + (2 * ((c.B - c.D) / PowerFunction(distance, 2)) * delta)
B := ((c.B + c.D) / 2) + ((c.D - c.B) * (PowerFunction(c.R0, 2) - PowerFunction(c.R1, 2)) / (2 * PowerFunction(distance, 2))) - (2 * ((c.A - c.C) / PowerFunction(distance, 2)) * delta)
return A, B
}
//Calculates C,D
//(x-c)*2 + (y-d)*2 = r*2 is second equation
func (c *RadiusFormOfCircle) CalculateCD() (float64, float64) {
distance := c.DistanceBetweenTwoCenters()
delta := c.CalculateDelta()
C := ((c.A + c.C) / 2) + ((c.C - c.A) * (PowerFunction(c.R0, 2) - PowerFunction(c.R1, 2)) / (2 * PowerFunction(distance, 2))) - (2 * ((c.B - c.D) / PowerFunction(distance, 2)) * delta)
D := ((c.B + c.D) / 2) + ((c.D - c.B) * (PowerFunction(c.R0, 2) - PowerFunction(c.R1, 2)) / (2 * PowerFunction(distance, 2))) + (2 * ((c.A - c.C) / PowerFunction(distance, 2)) * delta)
return C, D
}
//Calculates only X values (A,C)
// (x-a)*2 + (y-b)*2 = r*2 is one equation
//(x-c)*2 + (y-d)*2 = r*2 is second equation
func (c *RadiusFormOfCircle) CalculateXs() (float64, float64) {
distance := c.DistanceBetweenTwoCenters()
delta := c.CalculateDelta()
A := ((c.A + c.C) / 2) + ((c.C - c.A) * (PowerFunction(c.R0, 2) - PowerFunction(c.R1, 2)) / (2 * PowerFunction(distance, 2))) + (2 * ((c.B - c.D) / PowerFunction(distance, 2)) * delta)
C := ((c.A + c.C) / 2) + ((c.C - c.A) * (PowerFunction(c.R0, 2) - PowerFunction(c.R1, 2)) / (2 * PowerFunction(distance, 2))) - (2 * ((c.B - c.D) / PowerFunction(distance, 2)) * delta)
return A, C
}
// Calculates only Y Values (B,D)
// (x-a)*2 + (y-b)*2 = r*2 is one equation
//(x-c)*2 + (y-d)*2 = r*2 is second equation
func (c *RadiusFormOfCircle) CalculateYs() (float64, float64) {
distance := c.DistanceBetweenTwoCenters()
delta := c.CalculateDelta()
B := ((c.B + c.D) / 2) + ((c.D - c.B) * (PowerFunction(c.R0, 2) - PowerFunction(c.R1, 2)) / (2 * PowerFunction(distance, 2))) - (2 * ((c.A - c.C) / PowerFunction(distance, 2)) * delta)
D := ((c.B + c.D) / 2) + ((c.D - c.B) * (PowerFunction(c.R0, 2) - PowerFunction(c.R1, 2)) / (2 * PowerFunction(distance, 2))) + (2 * ((c.A - c.C) / PowerFunction(distance, 2)) * delta)
return B, D
}
// Calculates the Line Equation connecting both intersection points
func (circle *RadiusFormOfCircle) LineEquationConnectingTwoIntersectionPoint() string {
var a, b, c, d = circle.CalculateXY()
fmt.Println(a, b, c, d)
var numeratorConst = (b - d)
var denominatorConst = (a - c)
var output string
var slope = numeratorConst / denominatorConst
var constVal = b - (slope * a)
fmt.Println(numeratorConst, denominatorConst, slope, constVal)
if circle.DoesCircleIntersect() == true {
output = "y = " + FloatToString(slope) + " x + ( " + FloatToString(constVal) + " )"
} else {
output = "Circle doesn't Interesect"
}
return output
}
// Calculates the Line Equation connecting both centers
func (c *RadiusFormOfCircle) LineEquationConnectingTwoCenters() string {
var numeratorConst = (c.D - c.B)
var denominatorConst = (c.C - c.A)
var output string
var slope = numeratorConst / denominatorConst
var constVal = c.B - (slope * c.A)
if c.DoesCircleIntersect() == true {
output = "y = " + FloatToString(slope) + " x + ( " + FloatToString(constVal) + " )"
} else {
output = "Circle doesn't Interesect"
}
return output
}
//Checks if the two circle are tangential or not
func (c *RadiusFormOfCircle) IsTangent() bool {
isTangent := false
if (PowerFunction((c.A-c.C), 2)+(PowerFunction((c.B-c.D), 2)) == PowerFunction((c.R0-c.R1), 2)) || (PowerFunction((c.A-c.C), 2)+(PowerFunction((c.B-c.D), 2)) == PowerFunction((c.R0+c.R1), 2)) {
isTangent = true
}
return isTangent
}
//Check if the two circle has Outer Circle Tangency
func (c *RadiusFormOfCircle) IsOuterCircleTangency() bool {
isOuterCircleTangency := false
distanceBewtweenCircles := c.DistanceBetweenTwoCenters()
if (c.R0 + c.R1) == distanceBewtweenCircles {
isOuterCircleTangency = true
}
return isOuterCircleTangency
}
//Check if the two circle has Inner Circle Tangency
func (c *RadiusFormOfCircle) IsInnerCircleTangency() bool {
isInnerCircleTangency := false
distanceBewtweenCircles := c.DistanceBetweenTwoCenters()
if (c.R0-c.R1) > 0 && (c.R0-c.R1) == distanceBewtweenCircles {
isInnerCircleTangency = true
} else if (c.R0-c.R1) < 0 && (-(c.R0 - c.R1)) == distanceBewtweenCircles {
isInnerCircleTangency = true
}
return isInnerCircleTangency
}
// Point of tangency of two circles
func (c *RadiusFormOfCircle) TangentPoint() (float64, float64) {
denominator := (2 * (PowerFunction((c.C-c.A), 2) + PowerFunction((c.D-c.B), 2)))
radiusDifference := (PowerFunction(c.R0, 2) - PowerFunction(c.R1, 2))
x := (((c.A - c.C) * radiusDifference) / denominator) - ((c.A + c.C) / 2)
y := (((c.B - c.D) * radiusDifference) / denominator) - ((c.B + c.D) / 2)
return x, y
}
/*
This is for the general form
*/
// DistanceBetweenTwoCenters is used to calculate the distance between two circles
func (c *GeneralFormOfCircle) DistanceBetweenTwoCenters() float64 {
distance := (math.Sqrt(PowerFunction(c.A-c.D, 2) + PowerFunction(c.B-c.E, 2))) / 2
return distance
}
// Calculates the Line Equation connecting both intersection points
func (c *GeneralFormOfCircle) LineEquationConnectingTwoIntersectionPoint() string {
var output string
var denominator = (c.B - c.E)
var numOne = (c.D - c.A)
var numTwo = (c.F - c.C)
output = "y =" + FloatToString(numOne/denominator) + " x + ( " + FloatToString(numTwo/denominator) + " )"
return output
}
// Calculates the Line Equation connecting both intersection points
func (c *GeneralFormOfCircle) SlopeOfConnectingLineOfTwoIntersectionPoint() float64 {
var output float64
var denominator = (c.B - c.E)
var numOne = (c.D - c.A)
output = numOne / denominator
return output
}
/// Calculates the Line Equation connecting both centers
func (c *GeneralFormOfCircle) LineEquationConnectingTwoCenters() string {
var denominator = (c.D - c.A)
var numOne = (c.E - c.B)
output := "y = " + FloatToString(numOne/denominator) + " x" + FloatToString((((numOne/denominator)*c.A)-c.B)/2)
return output
}
//Checks if the two circle are tangential or not
func (c *GeneralFormOfCircle) IsTangent() bool {
isTangent := false
firstSqrt := math.Sqrt((PowerFunction(c.A, 2) + PowerFunction(c.B, 2) - (4 * c.C)))
secondSqrt := math.Sqrt((PowerFunction(c.D, 2) + PowerFunction(c.E, 2) - (4 * c.F)))
if PowerFunction((c.A-c.D), 2)+PowerFunction((c.B-c.E), 2) == PowerFunction((firstSqrt+secondSqrt), 2) || PowerFunction((c.A-c.D), 2)+PowerFunction((c.B-c.E), 2) == PowerFunction((firstSqrt-secondSqrt), 2) {
isTangent = true
}
return isTangent
}
// Point of tangency of two circles
func (c *GeneralFormOfCircle) TangentPoint() (float64, float64) {
u := (c.F - c.C)
v := (c.D - c.A)
w := (c.B - c.E)
denominator := 2 * (PowerFunction(v, 2) + PowerFunction(w, 2))
x := (2*u*v + c.A*PowerFunction(w, 2) + c.B*u*w) / denominator
y := (-2*u*w + c.B*PowerFunction(v, 2) + c.A*v*w) / denominator
return x, y
}
// Generate radius of both circle
func (c *GeneralFormOfCircle) RadiusOfTwoCircle() (float64, float64) {
sqrtOne := PowerFunction(c.A, 2) + PowerFunction(c.B, 2) - 4*c.C
sqrtTwo := PowerFunction(c.D, 2) + PowerFunction(c.E, 2) - 4*c.F
return sqrtOne, sqrtTwo
}
//Check weather two circle are not intersecting or touching eaching other
func (c *RadiusFormOfCircle) AreTwoNonIntersectingCircle() bool {
isNotInterescting := false
distance := c.DistanceBetweenTwoCenters()
if distance > (c.R0 + c.R1) {
isNotInterescting = true
}
return isNotInterescting
}