math & physics
I am trying to write an program for drawing 2D polygons(convex or non-convex)) with mouse.what exactly i am doing is first i will draw an triangle with 3 assigned vertices and by one mouse click get one vertex ,then by finding nearest two vertices of previous triangle i want to add new triangle. I am not getting idea to find nearest vertices .Please can anyone help ?
There's faster ways to do it, but you could always just compare each vertex to every other vertex at first. From the description of your app, it sounds unlikely that the complexity will cause any issues.
What in particular are you having trouble with? Do you know how to calculate distance? What language are you using?
I think your way to solve this problem is wrong, don't blame on me but i see this as an higly error prone procedure.
What if you clicked inside a triangle ? , finding the nearest vertices ??
this doesn't make sense unless you give a ref system , nearest to what ? your current point ? this is a large topic , normally you would use a kdtree to see which couple of point is nearest to another , i think you are overcomplcating the problem.
I would do like this, click on a point, you enter in a subroutine where the program expects to click on another screen coordinat, once you click it , repeat unti the last point is connected to the first within a range tollerance, before accepting the point i would check for self intersecting lines.
Honestly, if I were doing this (and I am), I'd just ask the user to click a bunch of points and use those as the vertex chain for the simply polygon. From there, just do a Delaunay triangulation, for which there's plenty of source code out there.
Of course, the user could enter a complex (self intersecting) polygon, but what you do with that is up to you and your application.
I recently did this for a chemistry project...
First Click on point(save index )
----Points indexed----(by connection)
then lookup the points location(x,y)..
if the polygon is growing as attached to a preexisting side, use relative distance to side midpoint, if not ignore,
treat the distacne pClick/(pMidpoint), pMouse as radius for the circumscribed circle, or inscribed which ever you prefer, refresh to screen the resulting polygons.
(i'd post code but that is not my intelectual property anymore..this bit is safe however)