java - Optimize algorithm to find intersection points in 2d roads (lines) -


i have list of lines represent roads such each road has "startingpoint" , "endingpoint". goal find "next" road of each road. a road next of road if starting point or ending point falls on top of starting point or ending point of road. example :

road : starting point of : (0,0) , ending point of (2,0)
road b : starting point of : (2,0) , ending point of (5,0)
road c: starting point of : (2,0) , ending point of (4,2)

so roads next :

a next { b , c}
b next { }
c next { }

my current algorthim doing in o(n^2) comparing every starting point of road starting , ending of road. how can make faster. think sorting roads might work i'm not sure. please tell me think!

note: saying use hashmap<start/endpoint,road> solution still o(n^2).

it depends want result. result of calculation of size o(#roads^2). means if want iterate on need o(#roads^2) @ best. being said if want able answer questions "return adjacencies of given road" can in o(#roads) algorithm implemented.


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -