Sub Class_Globals
Private Root As B4XView
Private xui As XUI
Private BaseLayer, DrawLayer As B4XView
Private BaseCV, DrawCV As B4XCanvas
Private bc, drawbc As BitmapCreator
Private shapeCount As Int
Private mode As String = "definition"
Private rx As B4XRect
End Sub
Public Sub Initialize
End Sub
Private Sub B4XPage_Created (Root1 As B4XView)
Root = Root1
rx.Initialize(0, 0, Root.Width, Root.Height)
BaseLayer = xui.CreatePanel("")
Root.AddView(BaseLayer, 0, 0, rx.Width, rx.Height)
DrawLayer = xui.CreatePanel("drawLayer")
Root.AddView(DrawLayer, 0, 0, rx.Width, rx.Height)
BaseCV.Initialize(BaseLayer)
DrawCV.Initialize(DrawLayer)
Dim bmp As B4XBitmap = xui.LoadBitmapResize(File.DirAssets, "picasso.jpg", rx.Width, rx.Height, True)
BaseCV.DrawBitmap(bmp, rx)
bc.Initialize(rx.Width, rx.Height)
bc.CopyPixelsFromBitmap(bmp)
drawbc.Initialize(rx.Width, rx.Height)
End Sub
Private Sub drawLayer_MouseClicked(ev As MouseEvent)
If mode = "definition" Then
Dim a() As Object = sampleColor(ev.X, ev.Y, 8)
Dim c() As Int = a(0)
Dim count As Float = a(1)
If count < 2 Then Return
shapeCount = shapeCount + 1
getBoundaries(ev.X, ev.Y, c)
If shapeCount = 4 Then
mode = "test" 'after defining 4 flowers it switches to test mode
End If
Else if mode = "test" Then
Dim index As Int = 16777216 + drawbc.GetColor(ev.X, ev.Y) 'the shape id # is encoded in the color of the shape in the BitmapCreator object
Select index
Case 1: xui.MsgboxAsync("You clicked on the orange flower - right bottom", "")
Case 2: xui.MsgboxAsync("You clicked on the red flower - right top", "")
Case 3: xui.MsgboxAsync("You clicked on the yellow flower- left top", "")
Case 4: xui.MsgboxAsync("You clicked on the blue flower- left bottom", "")
End Select
End If
End Sub
Private Sub getBoundaries(x As Int, y As Int, c() As Int)
Dim threshold As Float = 25
Dim delta As Float = 5
Dim path As BCPath
Dim pathcv As B4XPath
Dim previousRadius As Float
For i = 0 To 360 Step 10
Dim angle As Float = i
For j = 0 To 100
Dim radius As Float = j * delta
Dim i2 As Float = x + radius * CosD(angle)
Dim j2 As Float = y + radius * SinD(angle)
Dim a() As Object = sampleColor(i2, j2, 2)
Dim z() As Int = a(0)
If (Abs(z(0) - c(0)) > threshold) Or (Abs(z(1) - c(1)) > threshold) Or (Abs(z(2) - c(2)) > threshold) Then Exit
Next
If i = 0 Then
path.Initialize(i2, j2)
pathcv.Initialize(i2, j2)
previousRadius = radius
Else
If Abs(radius - previousRadius) / (1 + previousRadius) < 2 Then 'avoid anomalies spikes
path.LineTo(i2, j2)
pathcv.LineTo(i2, j2)
previousRadius = radius
End If
End If
Next
DrawCV.drawPath(pathcv, xui.Color_Black, False, 1)
drawbc.drawPath(path, -16777216 + shapeCount, True, 0)
End Sub
Private Sub sampleColor(x As Int, y As Int, sample As Int) As Object()
Dim delta As Int = sample
Dim argb As ARGBColor
Dim c(3) As Int
Dim count As Float
For i = Max(0, Min(X - delta, bc.mWidth -1)) To Max(0, Min(X + delta, bc.mWidth -1))
For j = Max(0, Min(Y - delta, bc.mHeight -1)) To Max(0, Min(Y + delta, bc.mHeight -1))
bc.GetARGB(i, j, argb)
count = count + 1
c(0) = c(0) + argb.r
c(1) = c(1) + argb.g
c(2) = c(2) + argb.b
Next
Next
For k = 0 To 2
c(k) = c(k) / count
Next
Return Array(c, count)
End Sub