Find 2 x values of the current y in a List(of Point)

橙三吉。 提交于 2021-01-28 06:17:11

问题


I have a List(of PointF). 2 nested for loops run through a rectangle that is stretched around a circle (or circle-ish (but is really is a closed path)). I want to work within this circle with GetPixel(x,y). That means, I need the respective minimum and maximum x value of the respective y. How can I work that out with this List(Of PointF)? The reason I'm requesting help here is because I get confused with these List.Find(predicates or matches)things.

For x As Integer = CInt(xmin) To CInt(xmax) Step 1
            For y As Integer = CInt(ymin) To CInt(ymax) Step 1
                'get the min and max value of x here
            Next      
Next

Edit: It's about: I wrote a program that uses gray images and Laplace to perform edge detection. A rectangle was drawn with the mouse and a search was made for edges within it. A Drawing2D.GraphicsPath was then created. This worked more or less well according to the object shown in the photo, because the rectangle had to be enlarged accordingly. Now my idea is that I draw a freehand GraphicsPath myself (instead of a rectangle) and let it search for edges within it. That's why I have this circle (or egg) and I have to make sure that I don't get over its borders. I could already work out xmin, xmax, ymin, and ymax, but not x1 and x2 (see drawing). So as I loop through all of the points from xmin to xmax and ymin to ymax, I want to know if the current point is inside the shape. I have to check whether its x value is between the shape's x range for the given y and then the reverse for the y value. That's my problem. How to use the List?


回答1:


I think you're better off not For-looping through the entire space. That would work but it's more effort than I think you need computationally.

Given a point, to determine whether or not it is within your shape - as defined by a list of point - you should only need to compare the defined point's x and y co-ords with the shape list.

Eg, Given a point x=100px and y=100px, you know that you have to check whether its x value is between the shape's x range for a the given y (100px) (or the reverse for the y value).

This would (probably?) only work for a simple shape that does not "turn in on itself" or has a curvature that doesn't change polarity. (Not sure what the mathematical name for that is, but there is one.) An hour-glass shape, for example, would probably present greater difficulty.

UPDATE

Here's a simple Linq approach which determines whether a specified point is within a defined shape. Note that it excludes points on the extremity of the shape but you can include these by changing ">" to ">=", etc.

For simplicity, I've used an octagon to approximate a circle. Also, the code assumes that ALL points within the bitmap are defined for the shape. That is, no interpolation is used.

    Dim s As List(Of Point) = New List(Of Point) From {New Point(50, 0), New Point(25, 25), New Point(0, 50), New Point(25, 75), New Point(50, 100), New Point(75, 75), New Point(100, 50), New Point(75, 25)}
    Dim p As Point = New Point(30, 75) ' Is inside the shape
    Dim IsInside As Boolean = s.Exists(Function(f) f.Y = p.Y AndAlso f.X < p.X) AndAlso s.Exists(Function(f) f.Y = p.Y AndAlso f.X > p.X)

There're probably ways to combine the Linq test, but you get the idea.




回答2:


Meanwhile, there are 2 answers: The solution by @SteveCinq and the solution by @Jimi.

Dim IsInside As Boolean = s.Exists(Function(f) f.Y = p.Y AndAlso f.X < p.X) AndAlso s.Exists(Function(f) f.Y = p.Y AndAlso f.X > p.X)
GraphicsPath.IsVisible() 


来源:https://stackoverflow.com/questions/65863671/find-2-x-values-of-the-current-y-in-a-listof-point

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!