Polarkoordinaten (Datenstrukturen)
Schreiben Sie ein Programm, dass Polarkoordinaten in Kartesische Koordinaten umwandelt und umgekehrt.
Dabei bezeichnen die Kartesischen Koordinaten K(x,\ y) einen Punkt in der Ebene. Die Polarkoordinaten werden mit P(r,\ \phi) bezeichnet. Dabei ist r der Abstand des Punktes zum Nullpunkt (K(0, 0)); \phi bezeichnet den Winkel im Gegenuhrzeigersinn ab Polarachse (= positive X-Achse).
Es gelten die folgenden Funktionen:
x = r \cos(\phi)
y = r \sin(\phi)
r = \sqrt{x^2 + y^2}
Interessant ist es noch, den Winkel \phi zu finden. Machen Sie sich dafür schlau zur Funktion "atan2".
0 Kommentare
6 Lösung(en)
#!/usr/bin/ruby
# -*- coding: utf-8 -*-
# @autor Philipp Gressly Freimann
# August 2011
#
# Aufgabe programmieraufgaben.ch
# Polarkoordinaten
def pi()
return -2*Math.atan2(-1, 0)
end
def hauptprogramm()
x, y = einlesenKartesisch()
x = x.to_f
y = y.to_f
r, phi = umrechnenNachPolar( x, y)
xNeu, yNeu = umrechnenNachKartesisch( r, phi)
ausgabePolar( r, phi)
ausgabeKartesisch(xNeu, yNeu)
end
def ausgabePolar(r, phi)
deg = phi * 180 / pi()
print "r=#{r}, phi=#{phi} (= #{deg}?)\n "
end
def ausgabeKartesisch(x, y)
print "(x, y) = (#{x}, #{y})\n"
end
def einlesenKartesisch()
print "x und y eingeben: "
return STDIN.gets.scan(/(-?\d*)[,; ](-?\d*)/).flatten
end
def umrechnenNachPolar(x, y)
r = Math.sqrt(x*x + y*y)
phi = Math.atan2(y, x)
if(phi > pi())
phi = phi - pi()
end
return r,phi
end
def umrechnenNachKartesisch(r, phi)
return r * Math.cos(phi), r * Math.sin(phi)
end
# start
hauptprogramm()
Lösung von: Philipp G. Freimann (BBW (Berufsbildungsschule Winterthur) https://www.bbw.ch)
package ch.programmieraufgaben.sequenz;
import java.util.Scanner;
/**
* Polarkoordinaten und Kartesische Koordinaten umrechnen
* @author Philipp Gressly (phi@gressly.ch)
*/
public class Polarkoordinaten {
public static void main(String[] args) {
new Polarkoordinaten().top();
}
class Paar {double a, b;} // (x,y) bzw. (r, phi)
void top() {
Paar xy, r_phi, xNeu_yNeu;
xy = einlesenKartesisch();
r_phi = umrechnenNachPolar(xy);
xNeu_yNeu = umrechnenNachKartesisch(r_phi);
ausgabePolar(r_phi);
ausgabeKartesisch(xNeu_yNeu);
}
void ausgabeKartesisch(Paar xNeu_yNeu) {
System.out.println("(x, y) = (" + xNeu_yNeu.a + ", " + xNeu_yNeu.b + ")");
}
void ausgabePolar(Paar r_phi) {
double r = r_phi.a;
double p = r_phi.b * 180 / Math.PI;
System.out.println("r = " + r + ", phi = " + p + " Grad");
}
Paar umrechnenNachKartesisch(Paar r_phi) {
Paar k = new Paar();
k.a = r_phi.a * Math.sin(r_phi.b);
k.b = r_phi.a * Math.cos(r_phi.b);
return k;
}
Paar umrechnenNachPolar(Paar xy) {
Paar polar = new Paar();
polar.a = Math.sqrt(xy.a * xy.a + xy.b * xy.b);
polar.b = Math.atan2(xy.a, xy.b);
if(polar.b > Math.PI) {
polar.b = polar.b - Math.PI;
}
return polar;
}
Paar einlesenKartesisch() {
Paar p = new Paar();
p.a = einlesen("x");
p.b = einlesen("y");
return p;
}
Scanner sc = new Scanner(System.in);
double einlesen(String frage) {
System.out.println("Bitte " + frage + " eingeben: ");
return sc.nextDouble();
}
} // end Polarkoordinaten
Lösung von: Philipp G. Freimann (BBW (Berufsbildungsschule Winterthur) https://www.bbw.ch)
// Autor: Andy Großhennig
// Solution for task: Polarkoordinaten (Datenstrukturen)
#include <iostream>
#include <math.h>
using namespace std;
/* Structures for parameters*/
struct polar
{
float r, a;
};
struct cartesian
{
float x, y;
};
// Function: Get the type of calculation and the requested parameters
void getCoordinate(polar *pPolar, cartesian *pCartesian, bool *cartesianAvailable)
{
pPolar->a = 0; pPolar->r = 0; pCartesian->x = 0; pCartesian->y = 0;
printf("Eingabe von Polarkoordinaten oder Kartesischen Koordinaten? (p, k): ");
switch(cin.get())
{
case 'k': printf("\n\nKoordinate auf der X-Achse angeben: ");
cin >> pCartesian->x;
printf("\n\nKoordinate auf der Y-Achse angeben: ");
cin >> pCartesian->y;
*cartesianAvailable = true;
break;
case 'p': printf("\n\nAbstand zum 0-Punkt angeben: ");
cin >> pPolar->r;
printf("\n\nVector zur Koordinate angeben: ");
cin >> pPolar->a;
*cartesianAvailable = false;
break;
default: printf("\n\nFehlerhafte eingabe!");
}
printf("\n\n");
}
// Function: Calculate the koordinates
void calculate(polar *pPolar, cartesian *pCartesian, bool &cartesianAvailable)
{
if(cartesianAvailable == true)
{
pPolar->r = sqrt((pCartesian->x * pCartesian->x) + (pCartesian->y * pCartesian->y));
pPolar->a = atan2 (pCartesian->x, pCartesian->y) * 180 / 3.14159265f;
printf("Die Koordinate befindet sich in einem Winkel von %.2f Grad in einer Entfernung von %.2f", pPolar->a, pPolar->r);
}
else
{
pCartesian->x = pPolar->r * cos(pPolar->a * 3.14159265f / 180);
pCartesian->y = pPolar->r * sin(pPolar->a * 3.14159265f / 180);
printf("Die Koordinaten befinden sich am Punkt X: %.2f, Y: %.2f", pCartesian->x, pCartesian->y);
}
}
// Function: Manage the calculation
void calculateCoordinate()
{
bool cartesianAvailable = true; //Cartesian or polar calculation
polar pPolar; /*Objects of the structures*/
cartesian pCartesian;
getCoordinate(&pPolar, &pCartesian, &cartesianAvailable);
calculate(&pPolar, &pCartesian, cartesianAvailable);
}
int main()
{
calculateCoordinate();
printf("\n\n");
system("Pause");
return 0;
}
Lösung von: Andy Großhennig (Bundeswehr)
function polar2cartesian(r, phi) {
return [r * Math.cos(phi), r * Math.sin(phi)];
}
function cartesian2polar(x, y) {
return [Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2)), Math.atan2(y, x)];
}
console.log(cartesian2polar(1, 1));
console.log(polar2cartesian(1, 1)); // lissalanda@gmx.at
Lösung von: Lisa Salander (Heidi-Klum-Gymnasium Bottrop)
// C++ 17
#include <iostream>
#define cos(x) cos((x) * (PI / 180))
#define sin(x) sin((x) * (PI / 180))
#define atan2(x, y) atan2(x, y) * (180 / PI)
constexpr double PI{ 3.1415926535897932384626433832795 };
template<class T = double>
class Coordinate {
T value_;
public:
explicit Coordinate(T v) : value_{ v } {}
T value() { return value_; }
};
using c_t = Coordinate<double>;
struct Polar {
c_t radius, phi;
};
struct Cartesian {
c_t x, y;
};
auto polar_to_cartesian(Polar polar) {
const auto r{ polar.radius.value() }, p{ polar.phi.value() };
return Cartesian{ c_t{ (r * cos(p)) }, c_t{ (r * sin(p)) } };
}
auto cartesian_to_polar(Cartesian cartesian) {
const auto x{ cartesian.x.value() }, y{ cartesian.y.value() };
return Polar{ c_t{ sqrt(pow(x, 2) + pow(y, 2)) }, c_t{ atan2(x, y) } };
}
int main() {
c_t x(1), y(1), r(1.41421), p(45);
auto car{ polar_to_cartesian(Polar{ r, p }) };
auto pol{ cartesian_to_polar(Cartesian{ x, y }) };
std::cout << car.x.value() << " " << car.y.value() << "\n";
std::cout << pol.radius.value() << " " << pol.phi.value() << "\n";
}
Lösung von: Jens Kelm (@JKooP)
using d = System.Double;
// Variante 1 -> mittels eigener Datentypen:
Cartesian PolarToCartesian(Polar p) => new(p.R * Math.Cos(p.A * (Math.PI / 180)), p.R * Math.Sin(p.A * (Math.PI / 180)));
Polar CartesianToPolar(Cartesian c) => new(Math.Sqrt(Math.Pow(c.X, 2) + Math.Pow(c.Y, 2)), Math.Atan2(c.X, c.Y) * 180 / Math.PI);
Console.WriteLine($"Datentypen:\nPolar in Kartesisch: {PolarToCartesian(new Polar(1.41421, 45))}");
Console.WriteLine($"Kartesisch in Polar: {CartesianToPolar(new Cartesian(1, 1))}");
// Variante 2 -> mittels Tupel
(d x, d y) P2C(d r, d a) => (r * Math.Cos(a * (Math.PI / 180)), r * Math.Sin(a * (Math.PI / 180)));
(d r, d a) C2P(d x, d y) => (Math.Sqrt(Math.Pow(x, 2) + Math.Pow(y, 2)), Math.Atan2(x, y) * 180 / Math.PI);
Console.WriteLine($"\nTupel:\nPolar in Kartesisch: {P2C(1.41421, 45)}");
Console.WriteLine($"Kartesisch in Polar: {C2P(1, 1)}");
public record struct Polar(d R, d A);
public record struct Cartesian(d X, d Y);
Lösung von: Jens Kelm (@JKooP)
Verifikation/Checksumme:
P(1.41421, 45?) = K(1, 1)
Aktionen
Neue Lösung hinzufügen
Bewertung
Durchschnittliche Bewertung:
Meta
Zeit: | |
Schwierigkeit: | k.A. |
Webcode: | ytev-arw8 |
Autor: | Philipp G. Freimann (BBW (Berufsbildungsschule Winterthur) https://www.bbw.ch) |