Printing Point Position Compared to the Rectangle

Let's begin from the easier part: printing the information about the point's position (Inside, Outside or Border). The code must look like this:

private void Draw()
    // Get the rectangle and point coordinates from the form
    var x1 = this.numericUpDownX1.Value;
    var y1 = this.numericUpDownY1.Value;
    var x2 = this.numericUpDownX2.Value;
    var y2 = this.numericUpDownY2.Value;
    var x = this.numericUpDownX.Value;
    var y = this.numericUpDownY.Value;

    // Display the location of the point: Inside / Border / Outside
    DisplayPointLocation(x1, y1, x2, y2, x, y);

private void DisplayPointLocation(
    decimal x1, decimal y1, decimal x2, decimal y2, decimal x, decimal y)
    var left = Math.Min(x1, x2);
    var right = Math.Max(x1, x2);
    var top = Math.Min(y1, y2);
    var bottom = Math.Max(y1, y2);
    if (x > left && x < right && …)
        this.labelLocation.Text = "Inside";
        this.labelLocation.BackColor = Color.LightGreen;
    else if (… || y < top || y > bottom)
        this.labelLocation.Text = "Outside";
        this.labelLocation.BackColor = Color.LightSalmon;
        this.labelLocation.Text = "Border";
        this.labelLocation.BackColor = Color.Gold;

The code above takes the coordinates of the rectangle and the point and checks whether the point is inside, outside or on the borders of the rectangle. By visulizing the result, the color of the background of the text block that contains it is changed.

Think about how to finish the uncompleted (on purpose) conditions in the if statements! The code above purposely doesn't compile, because the purpose is to make you think about how and why it works and finish on your own the missing parts.

results matching ""

    No results matching ""