Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
298 views
in Technique[技术] by (71.8m points)

vb.net - Accelerate program: How do I find xmin, xmax, ymin and ymax from a Drawing2D.GraphicsPath?

I'm drawing a Drawing2D.GraphicsPath freehand (?selbstgezeichneterPath“). This is definitely a simple, closed figure. From this, I only want to determine the vertical and horizontal extent of the shape. In yesterday's question, which was concerned with determining whether a point lies within the shape, I said that I know xmin, ..., but now I think my procedure is too slow. Is there a way to work out the 4 points faster?

find xmin, xmax, ymin and ymax to be able to draw a rectangle around the shape

Dim xmin As Single = 2000
        Dim xmax As Single = 0
        Dim ymin As Single = 2000
        Dim ymax As Single = 0

        For i As Integer = 0 To selbstgezeichneterPath.PointCount - 1 Step 1
            If selbstgezeichneterPath.PathPoints(i).X < xmin Then
                xmin = selbstgezeichneterPath.PathPoints(i).X
            End If
            If selbstgezeichneterPath.PathPoints(i).Y < ymin Then
                ymin = selbstgezeichneterPath.PathPoints(i).Y
            End If
            If selbstgezeichneterPath.PathPoints(i).X > xmax Then
                xmax = selbstgezeichneterPath.PathPoints(i).X
            End If
            If selbstgezeichneterPath.PathPoints(i).Y > ymax Then
                ymax = selbstgezeichneterPath.PathPoints(i).Y
            End If
        Next
question from:https://stackoverflow.com/questions/65871183/accelerate-program-how-do-i-find-xmin-xmax-ymin-and-ymax-from-a-drawing2d-gra

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Solved:

Dim xmin As Single = selbstgezeichneterPath.PathPoints.Min(Function(p) p.X)
Dim xmax As Single = selbstgezeichneterPath.PathPoints.Max(Function(p) p.X)
Dim ymin As Single = selbstgezeichneterPath.PathPoints.Min(Function(p) p.Y)
Dim ymax As Single = selbstgezeichneterPath.PathPoints.Max(Function(p) p.Y)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...