Dreiecksflächen (Unterprogramme)
Schreiben Sie die folgenden Funktionen zur Berechnung der Dreiecksfläche und verwenden Sie diese als Methoden in einem Hauptprogramm:
-
flaeche(s: real, hs: real)
Aus Grundseite s und der zugehörigen Höhe hs:
Fläche = {{s\ \times\ h_s}\over{2}}
-
flaeche(a: real, b: real, c: real)
Aus drei Seiten nach der Formel von Heron:
Fläche = \sqrt{s (s-a) (s-b) (s-c)}
Dabei bezeichnet s den halben Umfang. Schreiben Sie eine Hilfsmethode umfang(), die zunächst den Umfang des Dreiecks berechnet.
-
flaeche(ax: real, ay: real, bx: real, by: real, cx: real, cy: real)
Aus drei Eckpunkten. Dabei können die Seitenlängen einfach mit dem Satz des Pythagoras berechnet werden. Die Seite a z. B. wird durch
a = \sqrt{({c_x - b_x})^2 + ({c_y - b_y})^2}
ermittelt. Verwenden Sie nach der Ermittlung der drei Seiten die bereits geschriebene Funktion flaeche(a, b, c).
0 Kommentare
6 Lösung(en)
import java.util.Scanner;
public class Dreiecksflaechen {
public static void main(String[] args) {
new Dreiecksflaechen().top();
}
double flaeche;
void top() {
aufgabeGrundseiteHoehe();
aufgabeDreiSeiten();
aufgabeDreiEckpunkte();
}
void aufgabeGrundseiteHoehe() {
double s, hs;
System.out.println("Aus Grundseite und Höhe:");
s = eingabe("Grundseite");
hs = eingabe("Höhe");
flaeche = flaecheAusGrundseiteUndHoehe(s, hs);
ausgabe();
}
double flaecheAusGrundseiteUndHoehe(double s, double hs) {
return s * hs / 2.0;
}
void aufgabeDreiSeiten() {
double a, b, c;
System.out.println("Aus drei Seiten:");
a = eingabe("Seite a");
b = eingabe("Seite b");
c = eingabe("Seite c");
flaeche = flaecheAusDreiSeiten(a, b, c);
ausgabe();
}
double flaecheAusDreiSeiten(double a, double b, double c) {
// halber Umfang (Formel von Heron):
double s = (a + b + c) / 2.0;
return Math.sqrt(s * (s-a) * (s-b) * (s-c));
}
void aufgabeDreiEckpunkte() {
double ax, ay, bx, by, cx, cy;
System.out.println("Aus drei Eckpunkten:");
ax = eingabe("x-Koordinate von A");
ay = eingabe("y-Koordinate von A");
bx = eingabe("x-Koordinate von B");
by = eingabe("y-Koordinate von B");
cx = eingabe("x-Koordinate von C");
cy = eingabe("y-Koordinate von C");
flaeche = flaecheAusEckpunkten(ax, ay, bx, by, cx, cy);
ausgabe();
}
double flaecheAusEckpunkten(double ax, double ay, double bx, double by,
double cx, double cy) {
double a, b, c; // Seiten
a = abstand(bx, by, cx, cy);
b = abstand(ax, ay, cx, cy);
c = abstand(ax, ay, bx, by);
return flaecheAusDreiSeiten(a, b, c);
}
double abstand(double x1, double y1, double x2, double y2) {
double dx = x2 - x1;
double dy = y2 - y1;
return Math.sqrt(dx * dx + dy * dy);
}
Scanner sc = new Scanner(System.in);
double eingabe(String meldung) {
System.out.println(meldung + ": ");
return sc.nextDouble();
}
void ausgabe() {
System.out.println(); // newline
System.out.print ("Die Dreiecksfläche beträgt: ");
System.out.println(flaeche);
}
} // end of class Dreiecksflaechen
package ch.santis.übungen;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
new Main().top();
}
double flaeche;
void top(){
double s,hs,a,b,c,ax,ay,bx,by,cx,cy;
s = einlesen("s wert ");
hs = einlesen("hs wert ");
a = einlesen("a wert ");
b = einlesen("b wert ");
c = einlesen("c wert ");
ax = einlesen("ax wert ");
ay = einlesen("ay wert ");
bx = einlesen("bx wert ");
by = einlesen("by wert ");
cx = einlesen("cx wert ");
cy = einlesen("cy wert ");
System.out.println(flaeche(s,hs));
System.out.println(flaeche(a,b,c));
System.out.println(flaeche(ax,ay,bx,by,cx,cy));
}
Scanner sc = new Scanner(System.in);
double einlesen(String wert) {
System.out.println("Bitte " + wert + " eingeben: ");
return sc.nextDouble();
}
public double flaeche(double s, double hs){
return (s * hs) / 2;
}
public double flaeche(double a, double b, double c){
double s = (a + b + c) / 2;
return Math.sqrt(s * (s - a) * (s - b) * (s - c));
}
public double flaeche(double ay, double ax, double by, double bx, double cy, double cx){
double a,b,c;
a = Math.sqrt((cx - bx) * (cx - bx) + (cy - by) * (cy - by));
b = Math.sqrt((cx - ax) * (cx - ax) + (cy - ay) * (cy - ay));
c = Math.sqrt((ax - bx) * (ax - bx) + (ay - by) * (ay - by));
return flaeche(a,b,c);
}
}
Lösung von: Jesigan Sivalingam (Santis Training)
{$R+}
{$B+}
{FernUni Hagen}
{Hier ist die Lösunge für die ersten zwei Funktionen}
program FlaecheBerech(input, output);
{Eingabe 2 real zahlen}
var
EingabeS,
EingabeHS : real;
EingabeA,
EingabeB,
EingabeC : integer;
function umfangBR (a,b,c : integer) : real;
{Berechnung von umfang}
var
ErgebnisUmfang : real;
begin
ErgebnisUmfang := 0;
ErgebnisUmfang := (a+b+c)/2;
umfangBR := ErgebnisUmfang;
end; {Umfang Ende}
function flaeche (s,hs : real) : real;
{Berechnung von Dreiecksflaeche}
var
Ergebnis : real;
begin
Ergebnis := 0;
Ergebnis := (s * hs) / 2;
flaeche := Ergebnis;
end; {flaech Ende}
function HeronFlaeche (SeiteA,SeiteB,SeiteC : integer) :real;
{Berechnung von Dreiecksflaeche nach Heron}
var
HeronErgb,
Umfang : real;
begin
Umfang := 0;
Umfang := umfangBR(SeiteA,SeiteB,SeiteC);
HeronErgb := sqrt(Umfang *( (Umfang - SeiteA)*(Umfang - SeiteB)*(Umfang - SeiteC)) );
HeronFlaeche := HeronErgb;
end;
begin
EingabeS := 0;
EingabeHS := 0;
write('Grundseite: ');
readln(EingabeS);
write('Hoehe: ');
readln(EingabeHS);
write( flaeche(EingabeS, EingabeHS):2:2 );
writeln();
write('SeiteA: ');
read(EingabeA);
write('SeiteB: ');
read(EingabeB);
write('SeiteC: ');
read(EingabeC);
write('heron: ', HeronFlaeche(EingabeA, EingabeB,EingabeC):2:2 );
readln();
end.
Lösung von: Name nicht veröffentlicht
def umfang_dreieck(a,b,c):
return (a+b+c) / 2
def flaeche_dreieck(s,hs):
return s * hs / 2
def flaeche_dreieck_heron(a,b,c):
s = umfang_dreieck(a,b,c)
return (s*(s-a)*(s-b)*(s-c)) **(1/2)
def flaeche_dreieck_aus_koordinaten(ax,ay,bx,by,cx,cy):
c = ( (bx-ax) **2 + (cy-by) **2 ) **(1/2)
b = ( (c) **2 - (bx-ax) **2 ) **(1/2)
a = ( (c) **2 - (cy-by) **2 ) **(1/2)
#print(a,b,c)
return flaeche_dreieck_heron(a,b,c)
#print('c', (3 **2 + 4 **2) **0.5) # =5
print('A', flaeche_dreieck(3,4))
print('A', flaeche_dreieck_heron(3,4,5))
print('A', flaeche_dreieck_aus_koordinaten(1,1,4,1,4,5))
Lösung von: Alex Groeg (Freies Lernen)
class Triangle {
constructor(opts) {
this.a = opts.a; this.b = opts.b; this.c = opts.c;
this.s = opts.s; this.hs = opts.hs;
this.pA = opts.pA; this.pB = opts.pB; this.pC = opts.pC;
}
get circumference() { return this.a + this.b + this.c; }
get area() {
// aus grundseite und höhe
if (this.s && this.hs) return this.s * this.hs / 2;
// aus seitenlängen (hebron)
else if (this.a && this.b && this.c) {
let c = this.circumference / 2;
return Math.sqrt(c * (c-this.a) * (c-this.b) * (c-this.c));
}
// aus koordinaten der eckpunkte (pythagoras)
else try {
let
a = (this.pC[0]-this.pB[0])**2 + (this.pC[1]-this.pB[1])**2,
b = (this.pC[0]-this.pA[0])**2 + (this.pC[1]-this.pA[1])**2,
c = (this.pA[0]-this.pB[0])**2 + (this.pA[1]-this.pB[1])**2;
this.a = Math.sqrt(a); this.b = Math.sqrt(b); this.c = Math.sqrt(c);
return this.area;
}
catch { console.error('Nesufi?aj informoj por kalkuli areon.') }
}
}
// test
console.log( new Triangle({s:4, hs:3}).area );
console.log( new Triangle({a:3, b:4, c:5}).area );
console.log( new Triangle({pA:[5,3], pB:[9,2], pC:[9,5]}).area);
Lösung von: Lisa Salander (Heidi-Klum-Gymnasium Bottrop)
// C++ 14 | VS-2022
#include <iostream>
template<class T>
struct Coordinate {
T x, y;
};
template<class T>
class Triangle {
T base_, height_;
T side_a_, side_b_, side_c_;
Coordinate<T> coord_a_, coord_b_, coord_c_;
const T get_area_base() const noexcept;
const T get_area_heron() const noexcept;
const T get_area_coord();
const void print(T) const noexcept;
public:
Triangle(const T& base, const T& height) :
base_{ base }, height_{ height } { print(get_area_base()); }
Triangle(const T& side_a, const T& side_b, const T& side_c) :
side_a_{ side_a }, side_b_{ side_b }, side_c_{ side_c } { print(get_area_heron()); }
Triangle(const Coordinate<T>& coord_a, const Coordinate<T>& coord_b, const Coordinate<T>& coord_c) :
coord_a_{ coord_a }, coord_b_{ coord_b }, coord_c_{ coord_c } { print(get_area_coord()); }
};
template<class T>
const T Triangle<T>::get_area_base() const noexcept {
return base_ * height_ / 2;
}
template<class T>
const T Triangle<T>::get_area_heron() const noexcept {
const auto cir{ (side_a_ + side_b_ + side_c_) / 2 };
return sqrt(cir * (cir - side_a_) * (cir - side_b_) * (cir - side_c_));
}
template<class T>
const T Triangle<T>::get_area_coord() {
const auto dist{ [](const Coordinate<T>& a, const Coordinate<T>& b) { return sqrt(pow(b.x - a.x, 2) + pow(b.y - a.y, 2)); } };
side_a_ = dist(coord_b_, coord_c_);
side_b_ = dist(coord_a_, coord_c_);
side_c_ = dist(coord_a_, coord_b_);
return get_area_heron();
}
template<class T>
const void Triangle<T>::print(T val) const noexcept {
std::cout << "Flaeche: " << val << "\n";
}
int main() {
const Triangle<int> t1{ 4, 3 };
const Triangle<float> t2{ 3, 4, 5 };
const Triangle<double> t3{ {5, 2}, {9, 2}, {9, 5} };
}
Lösung von: Jens Kelm (@JKooP)
Verifikation/Checksumme:
Das folgende Dreieck (gegeben in drei Varianten) hat die Fläche 6:
s = 4, hs=3
a=3, b=4, c=5
ax=5, ay=2, bx=9, by=2, cx=9, cy=5
Aktionen
Neue Lösung hinzufügen
Bewertung
Durchschnittliche Bewertung:
Meta
Zeit: | 0.5 |
Schwierigkeit: | k.A. |
Webcode: | 4hpi-tuo2 |
Autor: | Philipp G. Freimann (BBW (Berufsbildungsschule Winterthur) https://www.bbw.ch) |