Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import std.math : sqrt, abs;
- import std.format : format;
- import std.stdio : writeln, writefln;
- struct Point {
- real x, y, z;
- this(real[] arr...) @safe pure nothrow {
- this.x = arr[0];
- this.y = arr[1];
- this.z = arr[2];
- }
- string toString() {
- return format!"(%s, %s, %s)"(this.x, this.y, this.z);
- }
- }
- struct Vector {
- real x, y, z;
- this(real[] arr...) @safe pure nothrow {
- this.x = arr[0];
- this.y = arr[1];
- this.z = arr[2];
- }
- this(in Point begin, in Point end) @safe pure nothrow {
- static foreach(ch; [ 'x', 'y', 'z' ])
- mixin("this." ~ ch ~ " = end." ~ ch ~ " - begin." ~ ch ~ ';');
- }
- real length() const @safe @nogc pure nothrow {
- return sqrt(this.x ^^ 2 + this.y ^^ 2 + this.z ^^ 2);
- }
- real squareLength() const @safe @nogc pure nothrow {
- return this.x ^^ 2 + this.y ^^ 2 + this.z ^^ 2;
- }
- ref Vector opUnary(string op)() {
- static if (op == "-") {
- static foreach (str; [ "this.x", "this.y", "this.z"])
- mixin(str ~ " = " ~ '-' ~ str ~ ';');
- return this;
- }
- else static if (op == "+")
- return this;
- else
- static assert(0, "Unary operator " ~ op ~ " is not supported for Vector.");
- }
- Vector opBinary(string op)(auto ref Vector other) const {
- static if (op == "+")
- return Vector(this.x + other.x, this.y + other.y, this.z + other.z);
- else static if (op == "-")
- return Vector(this.x - other.x, this.y - other.y, this.z - other.z);
- else static if (op == "*")
- return Vector(this.y * other.z - this.z * other.y, this.z * other.x - this.x * other.z, this.x * other.y - this.y * other.x);
- else
- static assert(0, "Binary operator " ~ op ~ " is not supported for Vector.");
- }
- Vector opBinary(string op, T : real)(auto ref T c) const {
- static if (op == "*")
- return Vector(c * this.x, c * this.y, c * this.z);
- else static if (op == "/")
- return Vector(this.x / c, this.y / c, this.z / c);
- else
- static assert(0, "Binary operator " ~ op ~ " is not supported for Vector.");
- }
- bool opEquals()(auto ref const Vector v) const {
- return this.x == v.x && this.y == v.y && this.z == v.z;
- }
- bool opCast(T : bool)() const {
- return this.x != 0 || this.y != 0 || this.z != 0;
- }
- string toString() {
- return format!"{%s, %s, %s}"(this.x, this.y, this.z);
- }
- }
- real scalarProduct()(auto ref Vector v1, auto ref Vector v2) @safe @nogc pure nothrow {
- return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
- }
- /* x1/x2 = y1/y2 = z1/z2 */
- bool isCollinear()(auto ref Vector v1, auto ref Vector v2) @safe pure nothrow {
- return (v1.x * v2.y == v2.x * v1.y) && (v2.y * v1.z == v1.y * v2.z);
- }
- struct Equation {
- real a, b, c, d;
- this(real[] arr...) @safe pure nothrow {
- this.a = arr[0];
- this.b = arr[1];
- this.c = arr[2];
- this.d = arr[3];
- }
- }
- struct Determinant {
- real value;
- this(real[][] vals) @safe pure nothrow {
- this.value = vals[0][0] * (vals[1][1] * vals[2][2] - vals[1][2] * vals[2][1])
- - vals[0][1] * (vals[1][0] * vals[2][2] - vals[1][2] * vals[2][0]) + vals[0][2] * (vals[1][0] * vals[2][1] - vals[1][1] * vals[2][0]);
- }
- }
- @safe unittest {
- auto d1 = Determinant([ [1, 1, 1], [2, 2, 2], [22, 40, 0] ]),
- d2 = Determinant([ [1, 0, 0], [2, 2, 0], [33, 3, 3] ]);
- assert(d1.value == 0);
- assert(d2.value == 6);
- }
- struct SystemOfEquations {
- private:
- Equation[] eqs;
- public:
- this(Equation[] arr...) @safe pure nothrow {
- assert(arr.length == 3);
- this.eqs = arr.dup;
- }
- Point solve() @safe pure nothrow {
- auto det = Determinant([ [eqs[0].a, eqs[0].b, eqs[0].c], [eqs[1].a, eqs[1].b, eqs[1].c], [eqs[2].a, eqs[2].b, eqs[2].c] ]),
- det_x = Determinant([ [eqs[0].d, eqs[0].b, eqs[0].c], [eqs[1].d, eqs[1].b, eqs[1].c], [eqs[2].d, eqs[2].b, eqs[2].c] ]),
- det_y = Determinant([ [eqs[0].a, eqs[0].d, eqs[0].c], [eqs[1].a, eqs[1].d, eqs[1].c], [eqs[2].a, eqs[2].d, eqs[2].c] ]),
- det_z = Determinant([ [eqs[0].a, eqs[0].b, eqs[0].d], [eqs[1].a, eqs[1].b, eqs[1].d], [eqs[2].a, eqs[2].b, eqs[2].d] ]);
- return Point(det_x.value / det.value, det_y.value / det.value, det_z.value / det.value);
- }
- }
- int main(string[] args) {
- auto A = Point(0, 0, -1),
- B = Point(0, 1, 0),
- D = Point(0, 2, -3),
- A1 = Point(1, 0, 2);
- byte a = 4, b = -1;
- double cos_phi = - 0.5;
- /* Task 1 */
- writeln("Задание 1.");
- auto l = (b + 1) ^^ 2 + (a + 1) ^^ 2 + 2 * (a + 1) * (b + 1) * cos_phi;
- writefln!"Ответ: |m + n| = sqrt(%s) = %s"(l, sqrt(l));
- /* Task 2 */
- writeln("\nЗадание 2.");
- auto AB = Vector(A, B),
- AM = (AB * a) / (a + 1);
- writefln!"AB = %s, AM = %s"(AB, AM);
- writefln!"Ответ: M(%s, %s, %s)"(AM.x + A.x, AM.y + A.y, AM.z + A.z);
- /* Task 3 */
- writeln("\nЗадание 3.");
- auto AD = Vector(A, D);
- writefln!"AD = %s"(AD);
- if (!AB.isCollinear(AD))
- writeln("Ответ: можно.");
- writefln!"|AD| = sqrt(%s) = %s, |AB| = sqrt(%s) = %s"(AD.squareLength, AD.length, AB.squareLength, AB.length);
- /* Task 4 */
- auto d1 = AB - AD,
- d2 = AB + AD;
- writeln("\nЗадание 4.");
- writefln!"Диагонали: d1 = AB - AD = %s, d2 = AB + AD = %s"(d1, d2);
- writefln!"|d1| = sqrt(%s) = %s, |d2| = sqrt(%s) = %s"(d1. squareLength, d1.length, d2.squareLength, d2.length);
- auto angle_cos = scalarProduct(d1, d2) / (d1.length * d2.length);
- writefln!"Ответ: arccos(%s/sqrt(%s)"(abs(scalarProduct(d1, d2)), d1.squareLength * d2.squareLength);
- /* Task 5 */
- auto s = AD * AB;
- writefln!"\nЗадание 5.\nОтвет: S = sqrt(%s) = %s"(s.squareLength, s.length);
- /* Task 6 */
- writeln("\nЗадание 6.");
- auto AA1 = Vector(A, A1);
- writefln!"AA1 = %s"(AA1);
- auto V = scalarProduct(s, AA1);
- writefln!"Ответ: AB x AD * AA1 = %s"(V);
- /* Task 7 */
- writeln("\nЗадание 7.");
- writefln!"Пусть s = AD x AB. Тогда s = %s."(s);
- auto tmp = scalarProduct(s, AA1);
- writefln!"s * AA1 = %s"(tmp);
- auto p = tmp / s.length;
- writefln!"|AH| = пр_(s)(AA1) = %s/sqrt(%s) = %s"(tmp, s.squareLength, p);
- writefln!"Длина s: sqrt(%s) = %s\nорт AH: %s"(s.squareLength, s.length, s / s.length);
- auto AH = s / s.length * p;
- writefln!"Ответ: AH = %s"(AH);
- /* Task 8 */
- writeln("\nЗадание 8.");
- auto eq1 = Equation(AB.x, AD.x, AA1.x, AH.x),
- eq2 = Equation(AB.y, AD.y, AA1.y, AH.y),
- eq3 = Equation(AB.z, AD.z, AA1.z, AH.z);
- auto sys = SystemOfEquations(eq1, eq2, eq3);
- auto tmp1 = sys.solve;
- writefln!"AH = %s, AB = %s, AD = %s, AA1 = %s"(AH, AB, AD, AA1);
- writefln!"Ответ: AH = %s * AB + %s * AD + %s * AA1"(tmp1.x, tmp1.y, tmp1.z);
- /* Task 9 */
- writeln("\nЗадание 9.");
- auto var = scalarProduct(AH, AA1);
- writefln!"Ответ: %s/sqrt(%s) = %s"(tmp, AA1.squareLength, tmp/AA1.length);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement