mawimawi
mawimawi

Reputation: 4343

Which algorithm to use for finding out whether a coordinate is in a specific area?

I want to know whether a point is in the "black" area of some image like the one below. visualisation

For the time being I created a large array like this (generated outside JavaScript):

area = [ [0,200], [0,201], [0,202], ..., [1,199], [1,200], ...]

to indicate which coordinates are black. Since this is getting very memory heavy for larger areas (I'm talking of image sizes of about 2000x2000 pixels), which kind of algorithm would you choose that is fast and not too memory hungry for finding out whether a specific coordinate is inside the black area?

Upvotes: 0

Views: 140

Answers (1)

Tobi
Tobi

Reputation: 524

You can draw the image to a canvas with same width and height as the image and then retrieve the pixelColor from the canvas at the specific point(x|y).

Here is a thread on how to retrieve the pixcel color: Get pixel color from an image

This is how i retrieve the pixel color from the mouseposition and return a colorcode('#rrggbb'):

var pixelData = canvas.getContext('2d').getImageData(event.offsetX, event.offsetY, 1, 1).data;
var hex= '#' + valToHex(pixelData[0]) + valToHex(pixelData[1]) + valToHex(pixelData[2]);

Upvotes: 2

Related Questions