﻿B4A=true
Group=Default Group
ModulesStructureVersion=1
Type=StaticCode
Version=12.8
@EndOfDesignText@

Sub Process_Globals
	Dim N As Int ' Pontok száma
	Dim points(10000, 2) As Float
	Dim hull(10000) As Int
	Dim hull_c As Int
	Dim hull_busy(10000) As Boolean
    Dim pi As Float = 3.14159265
	Dim limits(6) As Float
    Dim working As Boolean
End Sub



Sub add_points(x As Float, y As Float)
	
	points(N,0) = x
	points(N,1) = y
	n = N+1
	
	
	
	
End Sub

Sub reset
	hull_c = 0
	For t = 0 To hull_busy.length-1
		hull_busy(t) = False
	Next
	
End Sub


Sub way(acc As Float)

working = True
reset




Dim miny As Float = 99999999
Dim aktp As Int
For i = 0 To N - 1
    If points(i, 1) < miny Then
        aktp = i
        miny = points(i, 1)
    End If
Next

' A legalsó pontot kiemelni (piros szín)

add_hull(aktp, 0)


Dim rang As Float  = 2*pi 
	Dim minang_ind,minang_nolimit_ind As Int
	Dim minang_ang,minang_nolimit_ang,minang_nolimit_dis  As Float
Dim dis,dis0,dis1,ang As Float


'-------------------------------------------------------------- STANDARD FRAME
Do While True
    'keresse a legkozelebbit,aminek a legkisebb a szoge,
    minang_ind = -1
    minang_ang = 999999

    minang_nolimit_ind = -1
    minang_nolimit_ang = 999999
    minang_nolimit_dis = 999999

    For i = 0 To N - 1
        If i = aktp Then Continue
        dis0 = points(i, 0) - points(aktp, 0)
        dis1 = points(i, 1) - points(aktp, 1)
        dis = Sqrt(dis0 * dis0 + dis1 * dis1)
        ang = difang(rang, ATan2(dis0, dis1))


        If ang < minang_nolimit_ang And dis < minang_nolimit_dis Then
            minang_nolimit_ang = ang
            minang_nolimit_dis = dis
            minang_nolimit_ind = i
        End If

        If ang < minang_ang Then
            minang_ang = ang
            minang_ind = i

        End If

    Next 

    If minang_ind = -1 Then
        minang_ind = minang_nolimit_ind
        minang_ang = minang_nolimit_ang
    End If


    '    Line (points(aktp, 0), points(aktp, 1))-(points(minang_ind, 0), points(minang_ind, 1))

    If aktp = hull(0) And hull_c > 2 Then Exit 
    add_hull(aktp, hull_c)

    rang = ATan2(points(aktp, 0) - points(minang_ind, 0), points(aktp, 1) - points(minang_ind, 1))
    aktp = minang_ind


Loop





'-------------------------------------  FRAME RESOLUTION



Dim change ,nook As Boolean

Dim h1,h2,mindis_ind As Int
Dim mindis, dis0,dis1,dis3,dis4 As Float

Do While True  'For qqw = 0 To 100
    change = False
    h1 = 0
    Do While True
        h2 = (h1 + 1) Mod (hull_c - 0)

        mindis = 999999
        mindis_ind = -1
        
		
		
		dis3 = Sqrt(Power(points(hull(h2), 0) - points(hull(h1), 0),2)  + Power((points(hull(h2), 1) - points(hull(h1), 1)),2))
        For ind = 0 To N - 1
            If hull_busy(ind)  Then Continue
            dis0 = Sqrt(Power(points(ind, 0) - points(hull(h1), 0),2)  + Power((points(ind, 1) - points(hull(h1), 1)),2))
            dis1 = Sqrt(Power(points(ind, 0) - points(hull(h2), 0),2)  + Power((points(ind, 1) - points(hull(h2), 1)),2))

            dis4 = dis0 + dis1
            If dis3 * acc > dis4 And (dis4 < mindis) Then
                mindis = dis4
                mindis_ind = ind
            End If

        Next


        If mindis_ind <> -1 Then

            nook = add_hull(mindis_ind, h2)

            If nook  Then

                h1 = h1 + 1
                change = True
            End If

        End If


        h1 = h1 + 1
        If h1 > hull_c - 1 Then Exit 
    Loop
    If change = False Then Exit 
Loop

	calc_limits
working = False
End Sub













Sub add_hull (ind As Int, hova As Int ) As Boolean

    'mentes elkeszitese
    Dim mhull(hull_c+10) As Int
    For t = 0 To hull_c - 1
        mhull(t) = hull(t)
    Next

    'beiktat
    hull_c = hull_c + 1

    For t = hull_c - 2 To hova Step -1
        hull(t + 1) = hull(t)
    Next

    hull(hova) = ind


    '    GoTo 66

    'vizsgalat
    Dim ok As Boolean= True
	Dim felt As Boolean
	Dim t1,t3 As Int
    For t0 = 0 To hull_c - 1: t1 = (t0 + 1) Mod hull_c
        For t2 = 0 To hull_c - 1: t3 = (t2 + 1) Mod hull_c

            felt = (hull(t0) = hull(t2) Or hull(t0) = hull(t3))
            felt = (hull(t1) = hull(t2) Or hull(t1) = hull(t3)) Or felt

            If felt Then Continue
            If IsIntersection(points(hull(t0), 0), points(hull(t0), 1), points(hull(t1), 0), points(hull(t1), 1), points(hull(t2), 0), points(hull(t2), 1), points(hull(t3), 0), points(hull(t3), 1)) Then ok = False

        Next
    Next


    '    ok = 1


    If ok = False Then
        
        For t = 0 To hull_c - 1
            hull(t) = mhull(t)
        Next
        hull_c = hull_c - 1

    Else
        hull_busy(ind) = True
    End If


    Return ok




End Sub



Sub difang (angle1 As Float, angle2 As Float) As Float
    Dim diff As Float
    Dim c As Float =  2 * pi

    Do Until angle1 < c
        angle1 = angle1 - c
    Loop

    Do Until angle1 >= 0
        angle1 = angle1 + c
    Loop

    Do Until angle2 < c
        angle2 = angle2 - c
    Loop

    Do Until angle2 >= 0
        angle2 = angle2 + c
    Loop

    diff = angle1 - angle2

    If diff < -pi Then diff = diff + 2 * pi
    If diff > pi Then diff = diff - 2 * pi

    Return diff
End Sub 

Sub IsIntersection (X1 As Float, Y1 As Float, X2 As Float, Y2 As Float, X3 As Float, Y3 As Float, X4 As Float, Y4 As Float) As Boolean
    Dim denominator,ua,ub As Float
	
	denominator = (Y4 - Y3) * (X2 - X1) - (X4 - X3) * (Y2 - Y1)
    If denominator = 0 Then
        Return False 'IsIntersection = 0
    Else
        ua = ((X4 - X3) * (Y1 - Y3) - (Y4 - Y3) * (X1 - X3)) / denominator
        ub = ((X2 - X1) * (Y1 - Y3) - (Y2 - Y1) * (X1 - X3)) / denominator
        Return ua >= 0 And ua <= 1 And ub >= 0 And ub <= 1
    End If
End Sub


Sub bitmapcreation (pwidth As Int, pheight As Int) As Bitmap

    
    	
	Dim workbitmap As BitmapCreator
	workbitmap.Initialize(pwidth,pheight)



Dim x,y As Int

'Dim col As ARGBColor
'	col.r = 100
'	col.g = 0
'	col.b = 00
'	col.a = 255

'draw points 
For t = 0 To N-1
		x = pwidth/limits(2)*(points(t,0)-limits(0))
		y = pheight/limits(5)*(points(t,1)-limits(3))
		Log(x &" " & y)
		'workbitmap.SetARGB(x,y,col)
		workbitmap.DrawCircle(x,y,10,Colors.Red,True,1)
Next 


	'draw frames
Dim t2,x1,y1,x2,y2 As Int
	For t = 0 To hull_c - 1: t2 = (t + 1) Mod hull_c
		x1 = pwidth/limits(2)*(points(hull(t),0)-limits(0))
		y1 = pheight/limits(5)*(points(hull(t),1)-limits(3))
		x2 = pwidth/limits(2)*(points(hull(t2),0)-limits(0))
		y2 = pheight/limits(5)*(points(hull(t2),1)-limits(3))
		
		workbitmap.DrawLine(x1,y1,x2,y2,Colors.Black,5)
		
	Next
	




	Return workbitmap.bitmap
	
	
	
	
	
End Sub

Sub calc_limits
	Dim l As Float = 99999
	Dim limits(6) As Float = Array As Float (l,-l,0,l,-l,0)
	For t = 0 To n -1
		If points(t,0) < limits(0) Then limits(0) = points(t,0)
		If points(t,0) > limits(1) Then limits(1) = points(t,0)
	
		If points(t,1) < limits(3) Then limits(3) = points(t,1)
		If points(t,1) > limits(4) Then limits(4) = points(t,1)
		
	Next
	limits(2) = limits(1)-limits(0)
	limits(5) = limits(4)-limits(3)
	
	Log (limits(0))
	Log (limits(1))
	Log (limits(2))
	Log (limits(3))
	Log (limits(4))
	Log (limits(5))
	
	
	
End Sub