Here is the scenario we want to test:
– Write a “if else” statement that checks to see if bx and by are INSIDE the box. If the point (bx,by) is inside the box, print “Inside” otherwise print “Not inside”. (Hint use && in your answer)
– Write a “if else” statement that checks to see if bx and by are OUTSIDE the box. If the point (bx, by) is outside the box, print “Outside” otherwise print “Not outside”. (Hint use && in your answer)
– Test your IF statements with different values of bx and by
– See if you can make these if statements do the opposite by using the ! (not)
Values for the points:
x1,y1 = 100,100
x2, y2 = 120,150
bx, by = 115, 120
Here’s my solutions for it:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace lab4 { class Program { static void Main(string[] args) { Console.Title = "The Console - Lab 4 - IF statements"; bool returnTrue; bool notInside; float x1, y1, x2, y2, bx, by, cx, cy, dx, dy; x1 = 100; y1 = 100; x2 = 120; y2 = 150; bx = 115; by = 120; cx = 50; cy = 300; dx = 115; dy = 120; if (bx > x1 && bx < x2 && by > y1 && by < y2) { returnTrue = true; Console.WriteLine("Is bx,by inside the box = " + returnTrue); } else { returnTrue = false; Console.WriteLine("Is bx,by inside the box = " + returnTrue); } if (cx < x1 || cx > x2 || cy < y1 || cy > y2) { returnTrue = true; Console.WriteLine("Is cx,cy outside the box = " + returnTrue); } else { returnTrue = false; Console.WriteLine("Is cx,cy outside the box = " + returnTrue); } Console.WriteLine(); if (dx > x1 && dx < x2 && dy > y1 && dy < y2) { returnTrue = true; notInside = !returnTrue; Console.WriteLine("Is dx,dy inside the box = " + returnTrue); Console.WriteLine("Is dx,dy inside the box (not operator test) = " + notInside); } else { returnTrue = false; Console.WriteLine("Is bx,by inside the box = " + returnTrue); } Console.ReadKey(); } } }
Advertisements