Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace GeometryTasks{
- public class Vector{
- public double X, Y;
- //Длина вектора
- public double GetLength(){
- return Geometry.GetLength(this);
- }
- //Сумма этого вектора и аргумента
- public Vector Add(Vector vec){
- return Geometry.Add(this, vec);
- }
- //Принадлежит ли вектор отрезку
- public bool Belongs(Segment seg){
- return Geometry.IsVectorInSegment(this, seg);
- }
- }
- public class Segment{
- public Vector Begin;
- public Vector End;
- //Длина отрезка
- public double GetLength(){
- return Geometry.GetLength(this);
- }
- //Содержит ли отрезок вектор
- public bool Contains(Vector vec){
- return Geometry.IsVectorInSegment(vec, this);
- }
- }
- public class Geometry {
- //Длина вектора
- public static double GetLength(Vector vec) {
- return Math.Sqrt(vec.X * vec.X + vec.Y * vec.Y);
- }
- //Сумма векторов
- public static Vector Add(Vector v1, Vector v2) {
- return new Vector() { X = v1.X + v2.X, Y = v1.Y + v2.Y };
- }
- //Умножение вектора на число
- public static Vector Multiply(Vector vec, double k) {
- return new Vector() { X = vec.X * k, Y = vec.Y * k };
- }
- //Длина сегмента
- public static double GetLength(Segment segment) {
- //Длина вектора segment.Begin - segment.End
- return GetLength(Add(segment.Begin, Multiply(segment.End, -1)));
- }
- //Если точка в отрезке
- public static bool IsVectorInSegment(Vector vec, Segment segment) {
- return
- Math.Abs(
- GetLength(segment)
- - GetLength(new Segment() { Begin = vec, End = segment.End })
- - GetLength(new Segment() { Begin = segment.Begin, End = vec })
- ) <= double.Epsilon;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement