Inhalt
Die Fakten:
Plattform: | codewars.com |
Name: | Calculate the area of a regular n sides polygon inside a circle of radius r |
Level: | 6 kyu |
Sprache: | TypeScript |
Beschreibung:
Write the following function:
function areaOfPolygonInsideCircle(circleRadius, numberOfSides)
areaOfPolygonInsideCircle :: Double -> Int -> Double
areaOfPolygonInsideCircle circleRadius numberOfSides = undefined
function areaOfPolygonInsideCircle($circleRadius, $numberOfSides)
public static double AreaOfPolygonInsideCircle(double circleRadius, int numberOfSides)
(defn area-of-polygon-inside-circle [circle-radius number-of-sides]
def area_of_inscribed_polygon(circle_radius, number_of_sides):
def area_of_polygon_inside_circle(circle_radius, number_of_sides) do
def area_of_polygon_inside_circle(circle_radius, number_of_sides)
def area_of_polygon_inside_circle(circle_radius, number_of_sides)
static double areaOfPolygonInsideCircle(circleRadius, numberOfSides)
double areaOfPolygonInsideCircle (double circleRadius , int numberOfSides)
areaOfPolygonInsideCircle(_ circleRadius: Double, _ numberOfSides: Int) -> Double
double area_of_polygon_inside_circle(double circle_radius, int number_of_sides);
double area_of_polygon_inside_circle(double circle_radius, int number_of_sides);
double areaOfPolygonInsideCircle(double circleRadius, int numberOfSides)
areaOfPolygonInsideCircle = (circleRadius, numberOfSides)
export function areaOfPolygonInsideCircle(circleRadius: number, numberOfSides: number): number
public static double areaOfPolygonInsideCircle(double circleRadius, int numberOfSides)
areaOfPolygonInsideCircle = (circleRadius, numberOfSides)
func AreaOfPolygonInsideCircle(circleRadius float64, numberOfSides int) float64
area_of_polygon_inside_circle(circle_radius, number_of_sides)
fn area_of_polygon_inside_circle(circle_radius: f64, number_of_sides: i32) -> f64
It should calculate the area of a regular polygon of numberOfSides
, number-of-sides
, or number_of_sides
sides inside a circle of radius circleRadius
, circle-radius
, or circle_radius
which passes through all the vertices of the polygon (such circle is called circumscribed circle or circumcircle).
The answer should be a number rounded to 3 decimal places.
Input :: Output Examples
areaOfPolygonInsideCircle(3, 3) // returns 11.691
areaOfPolygonInsideCircle(5.8, 7) // returns 92.053
areaOfPolygonInsideCircle(4, 5) // returns 38.042
areaOfPolygonInsideCircle 3 3 -- returns 11.691
areaOfPolygonInsideCircle 5.8 7 -- returns 92.053
areaOfPolygonInsideCircle 4 5 -- returns 38.042
areaOfPolygonInsideCircle(3.0, 3) // returns 11.691
areaOfPolygonInsideCircle(5.8, 7) // returns 92.053
areaOfPolygonInsideCircle(4.0, 5) // returns 38.042
areaOfPolygonInsideCircle(3, 3) // returns 11.691
areaOfPolygonInsideCircle(5.8, 7) // returns 92.053
areaOfPolygonInsideCircle(4, 5) // returns 38.042
areaOfPolygonInsideCircle (3, 3) // returns 11.691
areaOfPolygonInsideCircle (5.8, 7) // returns 92.053
areaOfPolygonInsideCircle (4, 5) // returns 38.042
areaOfPolygonInsideCircle(3, 3) // returns 11.691
areaOfPolygonInsideCircle(5.8, 7) // returns 92.053
areaOfPolygonInsideCircle(4, 5) // returns 38.042
AreaOfPolygonInsideCircle(3, 3) // returns 11.691
AreaOfPolygonInsideCircle(5.8, 7) // returns 92.053
AreaOfPolygonInsideCircle(4, 5) // returns 38.042
area_of_inscribed_polygon(3, 3) # returns 11.691342951089922
area_of_inscribed_polygon(5.8, 7) # returns 92.05283874578583
area_of_inscribed_polygon(4, 5) # returns 38.04226065180614
area_of_polygon_inside_circle(3, 3) # returns 11.691
area_of_polygon_inside_circle(5.8, 7) # returns 92.053
area_of_polygon_inside_circle(4, 5) # returns 38.042
(area-of-polygon-inside-circle 3 3) ; returns 11.691
(area-of-polygon-inside-circle 5.8 7) ; returns 92.053
(area-of-polygon-inside-circle 4 5) ; returns 38.042
area_of_polygon_inside_circle(3, 3); // => 11.691
area_of_polygon_inside_circle(5.8, 7); // => 92.053
area_of_polygon_inside_circle(4, 5); // => 38.042
area_of_polygon_inside_circle(3, 3); // => 11.691
area_of_polygon_inside_circle(5.8, 7); // => 92.053
area_of_polygon_inside_circle(4, 5); // => 38.042
areaOfPolygonInsideCircle(3, 3); // => 11.691
areaOfPolygonInsideCircle(5.8, 7); // => 92.053
areaOfPolygonInsideCircle(4, 5); // => 38.042
area_of_polygon_inside_circle(3, 3) # returns 11.691
area_of_polygon_inside_circle(5.8, 7) # returns 92.053
area_of_polygon_inside_circle(4, 5) # returns 38.042
area_of_polygon_inside_circle(3, 3) # returns 11.691
area_of_polygon_inside_circle(5.8, 7) # returns 92.053
area_of_polygon_inside_circle(4, 5) # returns 38.042
areaOfPolygonInsideCircle(3, 3) # returns 11.691
areaOfPolygonInsideCircle(5.8, 7) # returns 92.053
areaOfPolygonInsideCircle(4, 5) # returns 38.042
areaOfPolygonInsideCircle(3, 3) // returns 11.691
areaOfPolygonInsideCircle(5.8, 7) // returns 92.053
areaOfPolygonInsideCircle(4, 5) // returns 38.042
areaOfPolygonInsideCircle(3, 3) // returns 11.691
areaOfPolygonInsideCircle(5.8, 7) // returns 92.053
areaOfPolygonInsideCircle(4, 5) // returns 38.042
AreaOfPolygonInsideCircle(3, 3) // returns 11.691
AreaOfPolygonInsideCircle(5.8, 7) // returns 92.053
AreaOfPolygonInsideCircle(4, 5) // returns 38.042
area_of_polygon_inside_circle(3, 3) ; returns 11.691
area_of_polygon_inside_circle(2, 4) ; returns 8
area_of_polygon_inside_circle(2.5, 5) ; returns 14.86
area_of_polygon_inside_circle(3.0, 3) // returns 11.691
area_of_polygon_inside_circle(2.0, 4) // returns 8
area_of_polygon_inside_circle(2.5, 5) // returns 14.86
Note: if you need to use Pi in your code, use the native value of your language unless stated otherwise.
Quelle: codewars.com
Lösung
Pseudo-Code
Wie immer gibt's reichlich Varianten, hier ist eine meiner.
Erst die Lösungsschritte in Pseudo-Code. Los geht’s:
Lösungsschritte
Schritt 1
Zuerst unterteilen wir das Polygon in einzelne Dreiecke.
Schritt 2
Dafür verbinden wir jeden Punkt auf dem Kreisumfang mit dem Mittelpunkt.
Schritt 3
Jede dieser Geraden hat die Länge des Radius.
Schritt 4
Mit dem Winkel zwischen den beiden bekannten Geraden können wir mit Sinus die Fläche eines Dreiecks berechnen.
Schritt 5
Statt des Winkels können wir auch den Radiant nehmen.
Schritt 6
Die Summe der Flächen aller Dreiecke ergibt die Fläche des Polygons.
Code
Geil. Übersetzen wir unseren Pseudo-Code in TypeScript:
Lösungsschritte
Meine erste Zeile (ich habe circleRadius
und numberOfSides
umbenannt:
export function areaOfPolygonInsideCircle(radius: number, numSides: number): number {
Als Erstes definieren wir den Radiant des ganzen Kreises (360°):
const RADIANT_CIRCLE = 2 * Math.PI;
Pi
sind 180°
, also sind 360° = 2 x Pi
.
Dann können wir den Radiant für jedes der Dreiecke berechnen:
const radiantAngle = RADIANT_CIRCLE / numSides;
Wir teilen einfach den Kreisradiant durch die Anzahl der Seiten des Polygons bzw. die Anzahl der Dreiecke. Jedes Dreieck hat den gleichen Winkel.
Alternativ würden wir in Grad hier 360° / numSides
rechnen.
Jetzt können wir die Fläche eines Dreiecks berechnen:
const areaTriangle = 0.5 * radius * radius * Math.sin(radiantAngle);
Hier noch mal die Formel zur Berechnung der Fläche eines Dreiecks: 1/2 x a x b x sin(ɣ)
Dann nur noch die Flächen aller Dreiecke zusammenrechnen:
const areaPolygon = areaTriangle * numSides;
Und das Ganze auf 3-Nachkommastellen gerundet zurückgeben:
return Number(areaPolygon.toFixed(3));
}
Voilá! 💪
Komplettlösung
export function areaOfPolygonInsideCircle(
radius: number,
numSides: number,
): number {
const RADIANT_CIRCLE = 2 * Math.PI; // all angles
const radiantAngle = RADIANT_CIRCLE / numSides; // each angle
const areaTriangle = 0.5 * radius * radius * Math.sin(radiantAngle);
const areaPolygon = areaTriangle * numSides;
return Number(areaPolygon.toFixed(3));
}