Tuesday, May 20, 2014

Struct_Exercise


namespace StructureExercise
{

    public partial class Form1 : Form
    {
        Rectangle rect;

        public Form1()
        {
            InitializeComponent();
            //.Net structs
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            rect = new Rectangle(7, 3, 400, 900);
        }

        private void btnDistanceBetweenPoint_Click(object sender, EventArgs e)
        {
            int xValue, yValue;
            if (int.TryParse(txtXvalue.Text, out xValue) && int.TryParse(txtYvalue.Text, out yValue))
            {
                Point UserPoint = new Point(xValue, yValue);
                double d = GetDistance(rect.Location, UserPoint);
                richTextBox1.AppendText(String.Format(" Distance Between Points({0:f}, {1:f}) \n and Upper Left Coner({2:f}, {3:f}) is = {4:f}",
                xValue, yValue, rect.Location.X, rect.Location.Y, d));
            }
        }

        public double GetDistance(Point p1, Point p2)
        {
            double a = p1.X - p2.X;
            double b = p1.Y - p2.Y;

            return Math.Sqrt(a * a + b * b);
        }

        private void btnChectPointLocation_Click(object sender, EventArgs e)
        {
            int xValue, yValue;
            if (int.TryParse(txtXvalue.Text, out xValue) && int.TryParse(txtYvalue.Text, out yValue))
            {
                Point UserPoint = new Point(xValue, yValue);
                if (UserPoint.X >= rect.Location.X && UserPoint.Y >= rect.Location.Y && UserPoint.X <= rect.Location.X + rect.Width && UserPoint.Y <= rect.Location.Y + rect.Height)
                {
                    MessageBox.Show("Point is inside");
                }
                else
                    MessageBox.Show("Point is outside");
            }
        }
    }
}
//Create the Rectangle rect. Use your own values
//Provide GUI for the user to enter the coordinates of a Point.

//1. Compute and display the distance between 
//this point and the upper left corner of the rectangle.

//2. Indicates / find out if the point p1 is inside or outside the rectangle

No comments:

Post a Comment