Advertisement
Graf_Rav

Untitled

Dec 17th, 2018
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.82 KB | None | 0 0
  1. using System;
  2.  
  3. namespace GeometryTasks{
  4.     public class Vector{
  5.         public double X, Y;
  6.         //Длина вектора
  7.         public double GetLength(){
  8.             return Geometry.GetLength(this);
  9.         }
  10.         //Сумма этого вектора и аргумента
  11.         public Vector Add(Vector vec){
  12.             return Geometry.Add(this, vec);
  13.         }
  14.         //Принадлежит ли вектор отрезку
  15.         public bool Belongs(Segment seg){
  16.             return Geometry.IsVectorInSegment(this, seg);
  17.         }
  18.     }
  19.    
  20.     public class Segment{
  21.         public Vector Begin;
  22.         public Vector End;
  23.         //Длина отрезка
  24.         public double GetLength(){
  25.             return Geometry.GetLength(this);
  26.         }
  27.         //Содержит ли отрезок вектор
  28.         public bool Contains(Vector vec){
  29.             return Geometry.IsVectorInSegment(vec, this);
  30.         }
  31.     }
  32.    
  33.     public class Geometry {
  34.         //Длина вектора
  35.         public static double GetLength(Vector vec) {
  36.             return Math.Sqrt(vec.X * vec.X + vec.Y * vec.Y);
  37.         }
  38.         //Сумма векторов
  39.         public static Vector Add(Vector v1, Vector v2) {
  40.             return new Vector() { X = v1.X + v2.X, Y = v1.Y + v2.Y };
  41.         }
  42.         //Умножение вектора на число
  43.         public static Vector Multiply(Vector vec, double k) {
  44.             return new Vector() { X = vec.X * k, Y = vec.Y * k };
  45.         }
  46.         //Длина сегмента
  47.         public static double GetLength(Segment segment) {
  48.             //Длина вектора segment.Begin - segment.End
  49.             return GetLength(Add(segment.Begin, Multiply(segment.End, -1)));
  50.         }
  51.         //Если точка в отрезке
  52.         public static bool IsVectorInSegment(Vector vec, Segment segment) {
  53.             return
  54.                 Math.Abs(
  55.                     GetLength(segment)
  56.                     - GetLength(new Segment() { Begin = vec, End = segment.End })
  57.                     - GetLength(new Segment() { Begin = segment.Begin, End = vec })
  58.                 ) <= double.Epsilon;
  59.         }
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement