Esempio n. 1
0
        public void checkCollisionWithShape(Shape shape)
        {
            if (this != shape)
            {
                if (pointsDistance(this.MiddlePoint, shape.MiddlePoint) <= this.Width / 2 + shape.Width / 2 && this.collisionChecked == false && shape.collisionChecked == false)
                {
                    int    temp1X = this.ShiftVector.X;
                    int    temp1Y = this.ShiftVector.Y;
                    int    temp2X = shape.ShiftVector.X;
                    int    temp2Y = shape.ShiftVector.Y;
                    double area1  = this.CalculateArea();
                    double area2  = shape.CalculateArea();

                    Vector2D vectorAfterCollision = new Vector2D();

                    vectorAfterCollision.X = (int)((temp1X * (area1 - area2) + 2 * area2 * temp2X) / (area1 + area2));
                    vectorAfterCollision.Y = (int)((temp1Y * (area1 - area2) + 2 * area2 * temp2Y) / (area1 + area2));
                    this.ShiftVector.X     = vectorAfterCollision.X;
                    this.ShiftVector.Y     = vectorAfterCollision.Y;
                    vectorAfterCollision.X = (int)((temp2X * (area2 - area1) + 2 * area1 * temp1X) / (area1 + area2));
                    vectorAfterCollision.Y = (int)((temp2Y * (area2 - area1) + 2 * area1 * temp1Y) / (area1 + area2));
                    shape.ShiftVector.X    = vectorAfterCollision.X;
                    shape.ShiftVector.Y    = vectorAfterCollision.Y;
                    this.collisionChecked  = true;
                    shape.collisionChecked = true;
                }
            }
        }
Esempio n. 2
0
 static void Main(string[] args)
 {
     for (;;)
     {
         Console.WriteLine("Enter Options :");
         Console.WriteLine("1. Circle 2. Rectangle 3. Triangle");
         int      choice          = Int32.Parse(Console.ReadLine());
         Shape    shape           = ShapesAreaCalculator.GetShape(choice);
         string   propertiesInput = Console.ReadLine();
         double[] properties      = Array.ConvertAll(propertiesInput.Split(' '), Double.Parse);
         shape.CalculateArea(properties).Display();
     }
 }