Win a copy of Zero to AI - A non-technical, hype-free guide to prospering in the AI era this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

I need a code that will check if point is lying inside rectangle

 
Vlad Timoshuk
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So rectangle and point formed by this test



I have aditionall class for points it is premade and should not be changed. i am adding it here for clarity



Ant this is main class where constructor that forms rectangle lies.


Update: So for clarity i need a code for method That will check if points formed BY test in the begining
   
Lie Inside rectangle that is formed by this test AND constructor from the main class
EDIT2: All Width and height Values in this code are Even, so it is ok if this code will return wrong results in not even values.
 
Campbell Ritchie
Marshal
Posts: 70604
287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you mean a single point? That should be easy enough as long as you know the coodinates of the point and the boundaries of the rectangle. It is more complicated if the rectangle doesn't have its sides vertical and horizontal. If you have a point at (10, 20), is that inside a rectangle (10, 10, 20, 20) or not?
 
Vlad Timoshuk
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I mentioned test in the begining this test forms points and BOUNDRIES of rectangle
 
Campbell Ritchie
Marshal
Posts: 70604
287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And what about my example where the point is on the boundary of the rectangle?
 
Carey Brown
Saloon Keeper
Posts: 7384
66
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aside from your bounds question, I noticed that some of your calculations are not returning the correct result. I wrote a simple test() to demonstrate.
The results were:
The width/height was off by one depending on whether the Rectangle's width/height was odd or even.

Note that I had to add toString() methods to your classes to run this, which is a habit you should get into.
 
Vlad Timoshuk
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Please don't edit posts after they have been answered.


Eng is not my Native lang and all the Ansvers here didnt ansver my quesiton so i though that i wrote it wrong and tried to be more specific.
 
Carey Brown
Saloon Keeper
Posts: 7384
66
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps you didn't understand Campbell's question: are the points inside the bounds, outside the bounds or ON the bounds?
 
Vlad Timoshuk
Greenhorn
Posts: 15
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:Perhaps you didn't understand Campbell's question: are the points inside the bounds, outside the bounds or ON the bounds?


There is whole set of points
If point genreated by test is lie inside bounds or ON bounds then it should return true
Otherwise false
 
Piet Souris
Bartender
Posts: 4102
156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To prevent the problems that Carey described, why don't you use doubles instead of ints?

And there are ready made classes that will do all the work for you: see Rectangle(2D), and Point(2D).
 
Carey Brown
Saloon Keeper
Posts: 7384
66
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't like the idea of introducing floating point operations into something that can be done entirely with integers.

The problem is with these two methods. Neither one of them addresses the case where N/2 may have a remainder. One or the other needs to be modified.
 
Piet Souris
Bartender
Posts: 4102
156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, it is up to OP of course, but I find working with ints pretty limiting.
 
Junilu Lacar
Sheriff
Posts: 15913
265
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The name isInside() is confusing. Look at other classes/interfaces like List, for example. List.contains(value) will return true if the list contains the given value. When you read myList.contains(someValue), it makes sense. However, in your code the logic is reversed. It says "rectangle is inside point."  You might want to rename that method to "contains" or "includes" or something to that effect.

I didn't read the entire thread yet but it would seem to me that you just need the coordinates of two diagonally opposite vertices of the rectangle to check if a point falls within or on its bounds.
 
Vlad Timoshuk
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:The name isInside() is confusing. Look at other classes/interfaces like List, for example. List.contains(value) will return true if the list contains the given value. When you read myList.contains(someValue), it makes sense. However, in your code the logic is reversed. It says "rectangle is inside point."  You might want to rename that method to "contains" or "includes" or something to that effect.

I didn't read the entire thread yet but it would seem to me that you just need the coordinates of two diagonally opposite vertices of the rectangle to check if a point falls within or on its bounds.



I cant change it, name of this method is premade and i must use it.
 
Vlad Timoshuk
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:I don't like the idea of introducing floating point operations into something that can be done entirely with integers.

The problem is with these two methods. Neither one of them addresses the case where N/2 may have a remainder. One or the other needs to be modified.


I aded it in original post, FOR the sake of this code Every width and heigh value is even so there will never be a reminder.
 
Campbell Ritchie
Marshal
Posts: 70604
287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Vlad Timoshuk wrote:. . . I aded it in original post, FOR the sake of this code Every width and heigh value is even so there will never be a reminder.

Kindly don't alter old posts like that; it makes the replies look stupid
 
Oscar Betgen
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
return contains(new Point(x,y));
 
Campbell Ritchie
Marshal
Posts: 70604
287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OB: Welcome to the Ranch
 
Who among you feels worthy enough to be my best friend? Test 1 is to read this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic