简体   繁体   English

使用哪种算法来确定坐标是否在特定区域?

[英]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. 我想知道一个点是否在某个图像的“黑色”区域,如下图所示。 可视化

For the time being I created a large array like this (generated outside JavaScript): 暂时我创建了一个像这样的大型数组(在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? 因为对于较大的区域来说这会变得非常重要(我说的是大约2000x2000像素的图像大小),你会选择哪种算法快速而且不需要太多内存来查找特定坐标是否在黑色内部区域?

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). 您可以将图像绘制到与图像具有相同宽度和高度的画布,然后在特定点(x | y)处从画布中检索pixelColor。

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'): 这是我从鼠标位置检索像素颜色并返回颜色代码('#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]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM