Git Product home page Git Product logo

Comments (9)

kewp avatar kewp commented on July 20, 2024

I suppose one way is just to loop through each point (site point) and check the distance and then the minimum one is the site the point belongs in

from voronoi.

mathiaswking avatar mathiaswking commented on July 20, 2024

"Not sure where to put these comments / requests for info"
Me neither, but this is fine I think :)

So, for a random point(x,y) you want to find what cell/site it belongs to?
I'm guessing the simplest way would be to go through each cell, and check if the point is outside or not:
for each site:
for each edge:
normal = site.pos - edge.pos[0] // pointing "inwards"
v = testpos - edge.pos[0]
if dot(normal, v) < 0: // The normals are opposite each other
break // it's outside of this cell

from voronoi.

kewp avatar kewp commented on July 20, 2024

I'm not sure I understand why a negative dot product means it is outside ?
I assume if you get to the end of the inner loop, you must be inside ?

from voronoi.

mathiaswking avatar mathiaswking commented on July 20, 2024

Sorry for the lack of indentation, github took all spaces there :/

Oops, a bit sloppy there by me. The normal of course should to be orthogonal to the edge.
diff = edge.pos[1] - edge.pos[0]
normal = point(-diff.y, diff.x) // pointing inwards

It's a check to see if a point is in front or behind a plane. In this case the plane is in 2D (an edge), and the front face of the plane is facing the site. If the dot product between two vectors is negative, it means they point in opposite directions in relation to the plane.

from voronoi.

kewp avatar kewp commented on July 20, 2024

Ok, I think I understand. So the only way a point can be inside the plane is if the vector to it (from a point in the plane, e.g. edge.pos[1]) has a positive dot product with the plane normal ...

Makes sense. Thanks for the help !

from voronoi.

kewp avatar kewp commented on July 20, 2024

I'm getting strange anomolies near the edges - maybe the point algorithm doesn't work there ?

image

from voronoi.

kewp avatar kewp commented on July 20, 2024

Here is my code

        int found=1;
    	while (edge) { 

    		jcv_point diff;
    		diff.x = edge->pos[1].x - edge->pos[0].x;
    		diff.y = edge->pos[1].y - edge->pos[0].y;
			
    		// pointing inwards
			jcv_point normal;
			normal.x = -diff.y;
			normal.y = diff.x; 
    		
    		jcv_point v;
    		v.x = x - edge->pos[0].x;
    		v.y = y - edge->pos[0].y;

    		double dot = normal.x * v.x + normal.y * v.y;
    		if (dot<0.0)
    		{
    			found=0;
    			break;
    		}

    		edge = edge->next;
    	}

from voronoi.

kewp avatar kewp commented on July 20, 2024

Think the algorithm is right. I used the distance algorithm I described earlier and I'm getting the same result.

I think the problem is that I'm plotting with dots so it inevitably looks aliased

from voronoi.

kewp avatar kewp commented on July 20, 2024

Yup. Looks better when I use smaller points image

from voronoi.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.