restructured
This commit is contained in:
parent
c495b7c47f
commit
66cefdfd63
204 changed files with 538 additions and 13662 deletions
38
seminar01_overload/07_vector3f/main.cpp
Normal file
38
seminar01_overload/07_vector3f/main.cpp
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
#include <iostream>
|
||||
#include "vector3f.h"
|
||||
using namespace std;
|
||||
|
||||
int main() {
|
||||
Vector3f a = {1.0, 2.0, -2.0};
|
||||
Vector3f b = {4.0, -1.0, 3.0};
|
||||
cout << "a = " << a << endl << "b = " << b << endl;
|
||||
cout << "a + b = " << a + b << endl;
|
||||
cout << "a - b = " << a - b << endl;
|
||||
|
||||
cout << "0.5 * a = " << 0.5 * a << endl;
|
||||
cout << "a * 0.5 = " << a * 0.5 << endl;
|
||||
|
||||
cout << "(a, b) = " << a * b << endl;
|
||||
|
||||
cout << "a / 5 = " << a / 5 << endl;
|
||||
|
||||
cout << "-a = " << -a << endl;
|
||||
cout << "+a = " << +a << endl;
|
||||
|
||||
cout << "a == b = " << (a == b) << endl;
|
||||
cout << "a != b = " << (a != b) << endl;
|
||||
|
||||
a += b;
|
||||
cout << "a += b: " << a << endl;
|
||||
|
||||
a -= b;
|
||||
cout << "a -= b: " << a << endl;
|
||||
|
||||
a *= 2;
|
||||
cout << "a *= 2: " << a << endl;
|
||||
a /= 2;
|
||||
cout << "a /= 2: " << a << endl;
|
||||
|
||||
normalize(a);
|
||||
cout << "normalize(a): " << a << " |a| = " << norm(a) << endl;
|
||||
}
|
||||
96
seminar01_overload/07_vector3f/vector3f.h
Normal file
96
seminar01_overload/07_vector3f/vector3f.h
Normal file
|
|
@ -0,0 +1,96 @@
|
|||
#pragma once
|
||||
#include <cmath>
|
||||
#include <iostream>
|
||||
|
||||
struct Vector3f {
|
||||
float x;
|
||||
float y;
|
||||
float z;
|
||||
};
|
||||
|
||||
Vector3f operator+(const Vector3f& a, const Vector3f& b) {
|
||||
Vector3f result = {a.x + b.x, a.y + b.y, a.z + b.z};
|
||||
return result;
|
||||
}
|
||||
|
||||
Vector3f operator-(const Vector3f& a, const Vector3f& b) {
|
||||
Vector3f result = {a.x - b.x, a.y - b.y, a.z - b.z};
|
||||
return result;
|
||||
}
|
||||
|
||||
Vector3f operator*(const Vector3f& a, float f) {
|
||||
Vector3f result = {f * a.x, f * a.y, f * a.z};
|
||||
return result;
|
||||
}
|
||||
|
||||
Vector3f operator*(float f, const Vector3f& a) {
|
||||
Vector3f result = {f * a.x, f * a.y, f * a.z};
|
||||
return result;
|
||||
}
|
||||
|
||||
int operator*(const Vector3f& a, const Vector3f& b) {
|
||||
return a.x * b.x + a.y * b.y + a.z * b.z;
|
||||
}
|
||||
|
||||
Vector3f operator/(const Vector3f& a, float f) {
|
||||
Vector3f result = {a.x / f, a.y / f, a.z / f};
|
||||
return result;
|
||||
}
|
||||
|
||||
Vector3f operator+(const Vector3f& a) {
|
||||
return a;
|
||||
}
|
||||
|
||||
Vector3f operator-(const Vector3f& a) {
|
||||
Vector3f result = {-a.x, -a.y, -a.z};
|
||||
return result;
|
||||
}
|
||||
|
||||
bool operator==(const Vector3f& a, const Vector3f& b) {
|
||||
if (a.x == b.x && a.y == b.y && a.z == b.z) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool operator!=(const Vector3f& a, const Vector3f& b) {
|
||||
return not (a == b);
|
||||
}
|
||||
|
||||
void operator+=(Vector3f& a, const Vector3f& b) {
|
||||
a = a + b;
|
||||
}
|
||||
|
||||
void operator-=(Vector3f& a, const Vector3f& b) {
|
||||
a = a - b;
|
||||
}
|
||||
|
||||
void operator*=(Vector3f& a, float f) {
|
||||
a = f * a;
|
||||
}
|
||||
|
||||
void operator/=(Vector3f& a, float f) {
|
||||
a = a / f;
|
||||
}
|
||||
|
||||
float squared_norm(const Vector3f& a) {
|
||||
return a * a;
|
||||
}
|
||||
|
||||
float norm(const Vector3f& a) {
|
||||
return sqrt(squared_norm(a));
|
||||
}
|
||||
|
||||
void normalize(Vector3f& a) {
|
||||
a /= norm(a);
|
||||
}
|
||||
|
||||
std::istream& operator>>(std::istream& in, Vector3f& a) {
|
||||
in >> a.x >> a.y >> a.z;
|
||||
return in;
|
||||
}
|
||||
|
||||
std::ostream& operator<<(std::ostream& out, const Vector3f& a) {
|
||||
out << "(" << a.x << ", " << a.y << ", " << a.z << ")";
|
||||
return out;
|
||||
}
|
||||
Reference in a new issue