Created
July 18, 2021 17:01
-
-
Save tahir-hassan/0b1720be42bdcc9c6a06fdd864f3e7f7 to your computer and use it in GitHub Desktop.
PowerShell code to get a circle from three points on an arc.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Point { | |
[double]$X; | |
[double]$Y; | |
Point([double]$x, [double]$y) { | |
$this.X = $x; | |
$this.Y = $y; | |
} | |
[Point]Add([double]$xDelta, [double]$yDelta) { | |
return [Point]::new($this.X + $xDelta, $this.Y + $yDelta); | |
} | |
[string]ToString() { | |
return "($($this.X),$($this.Y))"; | |
} | |
} | |
class Circle { | |
[string]$Name; | |
[Point]$Origin; | |
[double]$Radius; | |
Circle([double]$x, [double]$y, [double]$r) { | |
$this.Origin = [Point]::new($x, $y); | |
$this.Radius = $r; | |
} | |
[Point]GetPoint([double]$angle) { | |
$cosDelta = [Math]::Cos($angle) * $this.Radius; | |
$sinDelta = [Math]::Sin($angle) * $this.Radius; | |
return $this.Origin.Add($cosDelta, $sinDelta); | |
} | |
} | |
function GetCircleFromPoints( | |
[Point]$Point1, | |
[Point]$Point2, | |
[Point]$Point3) { | |
function Square([double]$x) { [double][Math]::Pow($x, 2.0) }; | |
$rhsTop = ((Square $Point1.X) - (Square $Point2.X) + (Square $Point1.Y) - (Square $Point2.Y))/2.0; | |
$rhsBottom = ((Square $Point2.X) - (Square $Point3.X) + (Square $Point2.Y) - (Square $Point3.Y))/2.0; | |
$topLeft = $Point1.X - $Point2.X; | |
$topRight = $Point1.Y - $Point2.Y; | |
$bottomLeft = $Point2.X - $Point3.X; | |
$bottomRight = $Point2.Y - $Point3.Y; | |
$originXCoeff = ($topLeft * $bottomRight) - ($bottomLeft * $topRight); | |
$originXCoeffValue = ($bottomRight * $rhsTop) - ($topRight * $rhsBottom); | |
$originX = $originXCoeffValue / $originXCoeff; | |
$originY = ((($Point1.X - $Point2.X) * $originX) - $rhsTop)/(-1 * ($Point1.Y - $Point2.Y)); | |
$radius = [Math]::Sqrt((Square ($Point1.X - $originX)) + (Square ($Point1.Y - $originY))); | |
return [Circle]::new($originX, $originY, $radius); | |
} | |
# test code: | |
$circ = [Circle]::new(20.0, 10.0, 8); | |
$circ.Name = 'Original'; | |
$point1 = $circ.GetPoint([Math]::PI * 0.1); | |
$point2 = $circ.GetPoint([Math]::PI * 0.25); | |
$point3 = $circ.GetPoint([Math]::PI * 0.35); | |
$circFromPoints = GetCircleFromPoints $point1 $point2 $point3; | |
$circFromPoints.Name = 'Derived'; | |
$circ, $circFromPoints | Format-Table; | |
<# | |
Name Origin Radius | |
---- ------ ------ | |
Original (20,10) 8 | |
Derived (20,10) 7.99999999999994 | |
#> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here is the same function in python: