Created
April 5, 2017 19:10
-
-
Save manureini/62a82125154dae7d23ca8d4e5c9b79b7 to your computer and use it in GitHub Desktop.
This file contains 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
//============================================================================ | |
// Name : Aufgabe1_1.cpp | |
// Author : | |
// Version : | |
// Copyright : Your copyright notice | |
// Description : Hello World in C++, Ansi-style | |
//============================================================================ | |
#include <iostream> | |
#include <cmath> | |
#include <algorithm> | |
#include <limits> | |
#include <float.h> | |
using namespace std; | |
int dim = 3; | |
struct Punkt { | |
long int x; | |
long int y; | |
}; | |
struct Rechteck { | |
Punkt lowLeft; | |
Punkt topRight; | |
}; | |
Punkt createPunkt(int pX, int pY) { | |
Punkt punkt; | |
punkt.x = pX; | |
punkt.y = pY; | |
return punkt; | |
} | |
Rechteck createRechteck(Punkt pLowLeft, Punkt pTopRight) { | |
Rechteck rechteck; | |
rechteck.lowLeft = pLowLeft; | |
rechteck.topRight = pTopRight; | |
return rechteck; | |
} | |
void printPunkt(Punkt pPunkt) { | |
// Punkt: x=10, y=20 | |
cout << "Punkt: x=" << pPunkt.x << ", y=" << pPunkt.y << endl; | |
} | |
double abstand(Punkt pPunkt1, Punkt pPunkt2) { | |
return sqrt(pow(pPunkt2.y - pPunkt1.y, 2) + pow(pPunkt2.x - pPunkt1.x, 2)); | |
} | |
bool gleich(const Punkt & pPunkt1, const Punkt & pPunkt2) { | |
return pPunkt1.x == pPunkt2.x && pPunkt1.y == pPunkt2.y; | |
} | |
double flaeche(const Rechteck * pRechteck) { | |
return (pRechteck->topRight.x - pRechteck->lowLeft.x) * (pRechteck->topRight.y - pRechteck->lowLeft.y); | |
} | |
bool innerhalb(const Rechteck & pRechteck, const Punkt & pPunkt) { | |
return pPunkt.x >= pRechteck.lowLeft.x && pPunkt.x <= pRechteck.topRight.x && pPunkt.y >= pRechteck.lowLeft.y | |
&& pPunkt.y <= pRechteck.topRight.y; | |
} | |
bool ueberlapp(const Rechteck * pRechteck1, const Rechteck * pRechteck2) { | |
return innerhalb(*pRechteck1, pRechteck2->lowLeft) || innerhalb(*pRechteck1, pRechteck2->topRight); | |
} | |
Punkt * neuerPunkt(void) { | |
Punkt *p = new Punkt(); | |
cout << "Neuer Punkt:" << endl; | |
cout << "x=" << endl; | |
cin >> p->x; | |
cout << "y=" << endl; | |
cin >> p->y; | |
return p; | |
} | |
Punkt ** neuesPunktFeld(int dim) { | |
Punkt **arr = new Punkt*[dim]; | |
for (int i = 0; i < dim; i++) { | |
arr[i] = neuerPunkt(); | |
} | |
return arr; | |
} | |
double maxAbstand(Punkt ** pArr, Punkt * pPunkt1, Punkt * pPunkt2) { | |
*pPunkt1 = *pArr[0]; | |
*pPunkt2 = *pArr[1]; | |
double distance = abstand(*pPunkt1, *pPunkt2); | |
for (int i = 0; i < dim; i++) { | |
for (int j = 0; j < dim; j++) { | |
double cdist = abstand(*pArr[i], *pArr[j]); | |
if (cdist > distance) { | |
*pPunkt1 = *pArr[i]; | |
*pPunkt2 = *pArr[j]; | |
distance = cdist; | |
} | |
} | |
} | |
return distance; | |
} | |
double minWeg(Punkt ** pInput, Punkt ** pOutput) { | |
double lenght = DBL_MAX; | |
sort(pInput, pInput + dim); | |
do { | |
double cLenght = 0; | |
for (int i = 0; i < dim - 1; i++) { | |
cLenght += abstand(*pInput[i], *pInput[i + 1]); | |
} | |
if (lenght > cLenght) { | |
lenght = cLenght; | |
for (int i = 0; i < dim; i++) { | |
pOutput[i] = pInput[i]; | |
} | |
} | |
} while (next_permutation(pInput, pInput + dim)); | |
return lenght; | |
} | |
void clearPunktFeld(Punkt ** pPArr) { | |
for (int i = 0; i < dim; i++) { | |
delete pPArr[dim]; | |
} | |
delete[] pPArr; | |
} | |
int main() { | |
/* Punkt punkt1 = createPunkt(0, 0); | |
Punkt punkt2 = createPunkt(10, 20); | |
Punkt punkt3 = createPunkt(10, 20); | |
printPunkt(punkt2); | |
cout << "Abstand: " << abstand(punkt1, punkt2) << endl; | |
cout << "P1 und P2 gleich: " << gleich(punkt1, punkt2) << endl; | |
cout << "P1 und P3 gleich: " << gleich(punkt1, punkt3) << endl; | |
cout << "P2 und P3 gleich: " << gleich(punkt2, punkt3) << endl; | |
Rechteck rechteck1 = createRechteck(punkt1, punkt2); | |
Rechteck rechteck2 = createRechteck(createPunkt(11, 21), createPunkt(30, 50)); | |
Rechteck rechteck3 = createRechteck(createPunkt(10, 20), createPunkt(30, 50)); | |
cout << "Fläche vom Rechteck: " << flaeche(&rechteck1) << endl; | |
cout << "(0,0) ist innerhalb Rechteck: " << innerhalb(rechteck1, createPunkt(0, 0)) << endl; | |
cout << "(99,1) ist innerhalb Rechteck: " << innerhalb(rechteck1, createPunkt(99, 1)) << endl; | |
cout << "(0,10) ist innerhalb Rechteck: " << innerhalb(rechteck1, createPunkt(0, 10)) << endl; | |
cout << "(11,21) ist innerhalb Rechteck: " << innerhalb(rechteck1, createPunkt(11, 21)) << endl; | |
cout << "(11,21) ist innerhalb Rechteck: " << innerhalb(rechteck1, createPunkt(11, 21)) << endl; | |
cout << "Rechteck1 überlappt Rechteck2: " << ueberlapp(&rechteck1, &rechteck2) << endl; | |
cout << "Rechteck1 überlappt rechteck3: " << ueberlapp(&rechteck1, &rechteck3) << endl; | |
Punkt *npunkt = neuerPunkt(); | |
printPunkt(*npunkt); | |
*/ | |
cout << "Anzahl Punkte:" << endl; | |
cin >> dim; | |
Punkt **parr = neuesPunktFeld(dim); | |
/*Punkt punkt1 = createPunkt(100, 0); | |
Punkt punkt2 = createPunkt(10, 0); | |
Punkt punkt3 = createPunkt(60, 0); | |
Punkt **parr = new Punkt*[dim]; | |
*/ | |
cout << "Punkte eingegeben:" << endl; | |
for (int i = 0; i < dim; i++) { | |
printPunkt(*parr[i]); | |
} | |
cout << endl; | |
Punkt* abstandPunkt1 = new Punkt(); | |
Punkt* abstandPunkt2 = new Punkt(); | |
cout << "maxAbstand: " << maxAbstand(parr, abstandPunkt1, abstandPunkt2) << endl; | |
printPunkt(*abstandPunkt1); | |
printPunkt(*abstandPunkt2); | |
Punkt **result = new Punkt*(); | |
cout << "minWeg: " << minWeg(parr, result) << endl; | |
clearPunktFeld(parr); | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment