For a math project I place 21 random points in a picture and after that I scan the surface to connect the dots via a line in a specific fashion. 2018R4 Windows10
This code runs in de Paint event of the Canvas
Dim PointRandom As New Random
Dim CanvasPicture As New Picture(Canvas1.Width,Canvas1.Height,32)
Dim X,Y,OldX,OldY As Integer
Dim CanvasSurface As RGBSurface
Dim PointsFound As Integer = 0
CanvasPicture.Graphics.ForeColor = RGB(0,0,0)
For i As Integer = 0 To 20
Dim PixelPoint As New OvalShape
PixelPoint.Width = 1
PixelPoint.Height = 1
X = PointRandom.InRange(0,Canvas1.Width)
Y = PointRandom.Inrange(0,Canvas1.Height)
CanvasPicture.Graphics.DrawObject(PixelPoint,X,Y)
Next
CanvasSurface = CanvasPicture.RGBSurface
For J As Integer = 0 To CanvasPicture.Width-1
For K As Integer = 0 To CanvasPicture.Height-1
If CanvasSurface.Pixel(J,K) = RGB(0,0,0) Then
PointsFound = PointsFound+1
End If
If PointsFound > 1 Then
CanvasPicture.Graphics.DrawLine(OldX,OldY,J,K)
End If
OldX = J
OldY = K
Next
Next
Canvas1.Backdrop = CanvasPicture
MsgBox(Str(PointsFound))
The RGBSurface.Pixel should return 21 pixels, but I got around 970? Looks like it detects more pixels with this RGB(0,0,0) value, but that shouldn’t be possible.
ps the code block doesn’t format the code anymore?
code block never formatted your code… it took it as-is when you paste it
as to your code… once PointsFound has a value, it looks like you just connect any dot that follows
should it be something like this?
For J As Integer = 0 To CanvasPicture.Width-1
For K As Integer = 0 To CanvasPicture.Height-1
If CanvasSurface.Pixel(J,K) = RGB(0,0,0) Then
PointsFound = PointsFound+1
If PointsFound > 1 Then
CanvasPicture.Graphics.DrawLine(OldX,OldY,J,K)
End If
OldX = J
OldY = K
end if
Next
Next
This of course “assumes” that the ONLY black points are the ones you placed.
[quote=429430:@Dave S]code block never formatted your code… it took it as-is when you paste it
as to your code… once PointsFound has a value, it looks like you just connect any dot that follows
should it be something like this?
For J As Integer = 0 To CanvasPicture.Width-1
For K As Integer = 0 To CanvasPicture.Height-1
If CanvasSurface.Pixel(J,K) = RGB(0,0,0) Then
PointsFound = PointsFound+1
If PointsFound > 1 Then
CanvasPicture.Graphics.DrawLine(OldX,OldY,J,K)
End If
OldX = J
OldY = K
end if
Next
Next
This of course “assumes” that the ONLY black points are the ones you placed.[/quote]
You are right - I detected it after I went through the code for the 50th time but your code doesn’t work either. It finds no black dots? But they are there - I can see them.
Shouldn’t you draw into a separate picture from the one you’re reading? I would think once you draw a line, that each pixel of that line will be counted as a hit in your loop.
Better yet, store the points in an array and loop through the array to draw the lines. If you draw the lines in a different color, you may draw over points and miss some when looking for a color.
I found out that RGBSurface.Pixel only works (Win10/2018R4) when the points are at least 3 pixels in width. A width < 3 is not detected. Antialias setting doesn’t change this. Neither does HiDPI.
As others have implicitely suggested, it’s probable that the pixels you are trying to paint are blending with the surrounding ones. I would think an OvalShape is prone to such blending, because its shape is not compatible with the square naturearrangement of pixels. I would try a RectShape instead.
[quote=430018:@Julen Ibarretxe Uriguen]As others have implicitely suggested, it’s probable that the pixels you are trying to paint are blending with the surrounding ones. I would think an OvalShape is prone to such blending, because its shape is not compatible with the square naturearrangement of pixels. I would try a RectShape instead.
Julen[/quote]
I tried with RectShape and it gives results with RectShape.Width and RectShape.Height = 2. If I use 1, then RGBSurface.Pixel doesn’t find it.
I created three pictures and drew an ovalshape, a rectsahpe and a single pixel on them. I am showing each picture in a listbox (each cell of the listbox corresponds to one pixel in the picture) and giving the hexadecimal value of the color of the 6,6 pixel in each of the three pictures (that pixel is where I place the ovalshape, the rectshape and the single pixel).
Seems to me that if all you need is 27 random points, you can just create 27 random co-ordinates, create variables of type POINT
and add them to an array.
Saves you even looking for them in a picture, you know exactly where they are