Click here to Skip to main content
15,881,600 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
How to get all points contained inside a 2d geometry?
How to know whether a point is contained withing a 2d geometry or not?
I need a built-in way, not complex math.
Posted
Updated 19-Aug-15 6:56am
v2
Comments
[no name] 19-Aug-15 12:23pm    
What is your geometry, e.g:?
a.) Triangle
b.) quadrangle
c.) Convex hull
d.) Any 3D object?

There are specalized (more easy) and general (more complex) solution
Ziya1995 19-Aug-15 12:55pm    
> What is your geometry, e.g:?
It is any 2d geometry, simply anything able to be a 2d geometry in WPF like rectangle, ellipse or random complex shapes. It can be a combination of 2 or more geometries. I can show a code sample of what is a 2d geometry in the context.

I need a built-in way to do it with any 2d geometry quick.
Is there a built-in way?

My assumptions:
- You are interesting in a 2D Scenario
- You have a set of N points (only intersting in case N > 3)

A General solution for this 2D is:
Determine the convex hull for your set of Points. How to do that you can find e.g. https://en.wikipedia.org/wiki/Quickhull[^]

Every Point which is not in the result of the convex hull is inside. Points of the convex hull are "at the border".

You can that extend also for 3D Scenarios.

I hope it helps.
Regards
 
Share this answer
 
Comments
Ziya1995 19-Aug-15 12:55pm    
No, you don't understand.
I need a built-in way as a ready method to do it.
It is WPF, a rich GUI framework and i need a ready to use way.
Is there a built-in way?
[no name] 19-Aug-15 13:01pm    
If you don't understand, than please answer the questions in my comment more precise. For Point in a square there is a build in function:
https://msdn.microsoft.com/en-us/library/ms557979(v=vs.110).aspx[^]
Ziya1995 19-Aug-15 13:06pm    
By saying "2d geometry" i mean anything able to be presented as 2d geometry, no matter rect, elllipse or complex random shapes.
I need a built-in way to get points or determine if a certain point is inside.
There is no solution to your question the way you want.

The function you wishes is highly specialized and you will not find it in a general purpose framework.

Your only way is to program complex math your self or find examples of code.
More or less, you have to transform your 2D arbitrary geometry into a geometry that have some special math properties that allow to check if a point is inside or not.
- the transform is to break the geometry into many convex parts
- the point have to be inside one of the parts.
 
Share this answer
 
v2
Comments
Ziya1995 20-Aug-15 14:45pm    
You are wrong.
I posted the solution (3).
Patrice T 20-Aug-15 15:13pm    
No, not wrong.
You asked for a complex question, and you post a solution to a very simplified problem to say I am wrong, it is not fair.

A rectangle is already a convex geometry, and a very simplified shape compared to a random complex form.
A rectangle with horizontal and vertical lines is an even simpler case.
Working on a limited field (100*100) is also a simplification.
Ziya1995 21-Aug-15 2:26am    
I don't understand you.
My code calculates points for the Geometry class, not for a rectangle. It means it can calculate points for any complex geometry in the same way. No matter how complex geometries are, it can calculate it, because they are still instances of the Geometry class.
I can change RectangleGeometry to EllipseGeometry in the same way.
I can group many geometries to form an instance of the Geometry class and then calculate it in the same way.

My code uses RectangleGeometry for simplification to make it easier to understand.
Now, can you explain what is wrong?
Patrice T 21-Aug-15 16:57pm    
The Geometry class is hiding the complexity of maths, but the complexity is there.
And the Geometry class is highly specialised piece of code.
If the shapes allowed is a combination of shapes like Circle, Ellipsis, Triangle, Square, Rectangle and Pentagon, they are all convex, and you jobs of combinig them to a complex shape is the tricky part of the process done by you.
Example: the shape is a star.
If you take a Pentagon and 5 Triangles to make it, you do the difficult job of breaking the shpe to a lot of convex shapes.
If you simply give the 10 points of the polygon defining the star, the class is doing the difficult job of breaking the polygon into a set of convex shapes.
As an exercice, try to set the shape of a horse shoe "C", if the Geom class can deal with it, try to follow what it does, and you will see complexity.
Ziya1995 24-Aug-15 3:11am    
> if the Geom class can deal with it
Yes, it deals, however slower.
I don't understand you, you mean FillContains can't check whether a point inside or not at complex geometries? If so you can write a geometry and send me a code and i check out. We also can do next: i create a complex geometry as ellipse with a hole inside.
But i can't create a horse shoe geometry, i can't draw it.

> try to follow what it does, and you will see complexity.
What complexity? My app will get frozen when i call FillContains of a complex geometry?
Can you say exactly what you mean?
Yes, it is possible.
Geometry.FillContains Method (Point)[^]
"points" variable is a list of all points contained within a 2d geometry (geom).
Code:
C#
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        this.Loaded += MainWindow_Loaded;
    }

    void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        RectangleGeometry rectgeom = new RectangleGeometry(new Rect(0, 0, 10, 10));
        Geometry geom = rectgeom;

        List<Point> points = GetPoints(geom); // Points contained within a 2d geometry
    }

    private List<Point> GetPoints(Geometry geom)
    {
        List<Point> points = new List<Point>();

        for (int i1 = 0; i1 < 100; i1++)
            for (int i2 = 0; i2 < 100; i2++)
                if (geom.FillContains(new Point(i1, i2))) points.Add(new Point(i1, i2));

        return points;
    }
}
 
Share this answer
 
v3
Comments
Patrice T 20-Aug-15 14:50pm    
This solution is only for geometry that is rectangular and with horizontal and vertical lines only.
A far cry from "random complex geometry"
Think of your geometry as a horse shoe shape, and see how it fit your program.
Ziya1995 21-Aug-15 2:35am    
I don't understand you.
It calculated it for the Geometry class, not for the RectangleGeometry class.
It can calculate it for any instance of the Geometry class no matter how complex the instance is.
Can i make you sure, if i post a code where it calculates points for your geometry you write a code for to me?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900