All pastes #1788553 Raw Edit

Calc Distance of Broken Line

public text v1 · immutable
#1788553 ·published 2010-02-07 00:09 UTC
rendered paste body
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace Line
{
    class Point
    {
        private int x;
        private int y;
        public int X { get {return x;}}
        public int Y { get {return y;}}
        public Point(int X,int Y)
        {
            x = X;
            y = Y;
        }
    }
    class BrokenLine
    {
        List<Point> brokenLine;
        public BrokenLine()
        {
            brokenLine = new List<Point>();
        }
        public void AddPoint(Point point) 
        {
            brokenLine.Add(point);
        }
        public void AddPoint(int X,int Y)
        {
            this.AddPoint(new Point(X,Y));
        }
        private double CalcDistanceBetweenPoints(Point p1, Point p2)
        {
            return Math.Sqrt(Math.Pow(p2.X - p1.X, 2) + Math.Pow(p2.Y - p1.Y, 2));
        }
        private double CalcDistance()
        {
            double distance = 0;
            for (int i = 1; i < brokenLine.Count; ++i)
            {
               distance += CalcDistanceBetweenPoints(brokenLine[i - 1], brokenLine[i]);
            }
            return distance;
        }
        public double Distance
        {
            get
            {
                return CalcDistance();
            }
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            BrokenLine brokenLine = new BrokenLine();
            brokenLine.AddPoint(1, 2);
            brokenLine.AddPoint(1, 3);
            Console.WriteLine(brokenLine.Distance);
        }
    }
}