Android Question How is this possible?

Tayfur

Well-Known Member
Licensed User
Longtime User
Untitled.png



How is change my variable ("a1"). I cant under stand???
how is connect variable of "a1" and "yy"

B4X:
#Region  Project Attributes
    #ApplicationLabel: my code
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    'Private a1,a2,a3,a4,c1,c2,c3,c4,d1,p1,s1,yy As Point
    Private a1 As Point
    Private a2 As Point
    Private a3 As Point
    Private a4 As Point
    Private yy As Point
    Private d1 As Point
    Private d2 As Point
    Private c1 As Point
    Private c2 As Point
    Private c3 As Point
    Private c4 As Point
    Private s1 As Point
    Private sayac As Int
    Private pnl As Panel
    Private ft As Boolean
    Private lbl As Label
    Type Point(X1 As Float,Y1 As Float,X2 As Float,Y2 As Float,Lines As Boolean,m As Float,n As Float)
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    pnl.Initialize("pnl")
    lbl.Initialize("lbl")
    Activity.AddView(lbl,0,0,100%x,20%y)
    Activity.AddView(pnl,0,20%y,100%x,80%y)
    yy.Lines=False
    a1.Initialize
    a2.Initialize
    a3.Initialize
    a4.Initialize
    d1.Initialize
    a1=do_ZERO
    a2=do_ZERO
    a3=do_ZERO
    a4=do_ZERO
    c1=do_ZERO
    c2=do_ZERO
    c3=do_ZERO
    c4=do_ZERO
    d1=do_ZERO
    s1=do_ZERO
    ft =False
    sayac=0
End Sub

Sub do_ZERO() As Point
    Dim lns As Point
    lns.Initialize
    lns.Lines=False
    lns.X1=0
    lns.Y1=0
    lns.X2=0
    lns.Y2=0
    lns.m=0
    lns.m=0
    Return lns
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub
Sub pnl_Touch (Action As Int, X As Float, Y As Float)
    If Action<>1 Then Return
    lbl.Text="X : "&X&" || Y : "&Y
If ft=False   Then
        yy.X1=X
        yy.Y1=Y
        ft=True
    Else
        Log("1.....>>:"&a1)
        yy.X2=X
        yy.Y2=Y
        Log("2.....>>::"&a1)
        Dim qq,ww As Int
        qq=Sqrt(yy.X1*yy.X1+yy.Y1*yy.Y1)
        Log("3.....>>::"&a1)
        ww=Sqrt(yy.X2*yy.X2+yy.Y2*yy.Y2)
        Log("4.....>>::"&a1)
        'Log("SQR:"& Sqrt(yy.X1*yy.X1+yy.Y1*yy.Y1)&"<"&Sqrt(yy.X2*yy.X2+yy.Y2*yy.Y2))
        'Log("yedek a2:"&a1)
        If qq > ww Then
            Dim aq1,aq2,aq3,aq4 As Float
            aq1=yy.X1
            aq2=yy.Y1
            aq3=yy.X2
            aq4=yy.Y2
            yy.X1=aq3
            yy.Y1=aq4
            yy.X2=aq1
            yy.Y2=aq2
        End If
      
        yy.m=(yy.Y2-yy.Y1)/(yy.X2-yy.X1)
        yy.n=yy.Y2-yy.m*yy.x2
        yy.Lines=True
      
        Dim ff As Point
        ff=yy
        sayac=sayac+1
        Select sayac
            Case 1
                a1=ff
                'Log("a1:"&a1)
                Line_draw(a1,Colors.red,3)
            Case 2
                a2=ff
                Line_draw(a2,Colors.red,3)
            Case 3
                a3=ff
                'Log("a3")
                Line_draw(a3,Colors.red,3)
            Case 4
                a4=ff
                'Log("a4")
                Line_draw(a4,Colors.red,3)
              
        End Select
      
        Select sayac
            Case 2
                Circle_Draw(a1,a2)
            Case 3
                Circle_Draw(a1,a2)
                Circle_Draw(a1,a3)
                Circle_Draw(a2,a3)
            Case 4
                Circle_Draw(a1,a2)
                Circle_Draw(a1,a3)
                Circle_Draw(a1,a4)
                Circle_Draw(a2,a3)
                Circle_Draw(a2,a4)
                Circle_Draw(a3,a4)
        End Select
            ft=False
          
End If
  
End Sub
Sub Circle_Draw(line1 As Point,line2 As Point)
'    Formülümüz K=[ (Dy-Cy)(Cx-Ax) - (Cy-Ay)(Dx-Cx) ] / [ (Dy-Cy)(Bx-Ax) - (By-Ay)(Dx-Cx) ]
'
'bulduğumuz K değerini E kesişim noktasını bulmak için kullanıyoruz.
'Ex=Ax + (Bx-Ax)*K
'Ey=Ay + (By-Ay)*K
    Dim K,k1,k2 As Float
    Dim X, Y As Float
    'Log("line1:"& line1)
    'Log("line2:"& line2)
    K=( (line2.Y2-line2.Y1)*(line2.X1-line1.X1) - (line2.Y1-line1.Y1)*(line2.X2-line2.X1) ) / ( (line2.Y2-line2.Y1)*(line1.X2-line1.X1) - (line1.Y2-line1.Y1)*(line2.X2-line2.X1) )
    X=line1.X1 + (line1.X2-line1.X1)*K
    Y=line1.Y1 + (line1.Y2-line1.Y1)*K
    Dim cnv As Canvas
    cnv.Initialize(pnl)
    cnv.DrawCircle(X,Y,5dip,Colors.Green,False,5)
    pnl.Invalidate
    lbl.Text=lbl.Text&CRLF&X&" - "&y&CRLF&"Tanjant:"&Aci_Bul(line1,line2)
  
    d1=Dik_doru_Ciz(X,Y,line1)
'***************dik doğruyu olşturm*************************************
'Log("ACI:"&Aci_Bul(d1,line2))
'Log("ACI:"&Aci_Bul(line2,d1))
s1=Simtrik_dogru(d1,line2)
'***********************************************************  
End Sub
Sub Simtrik_dogru(Ref_dogru As Point,Kesen_dogru As Point) As Point
    Dim sd As Point
    sd.Initialize
    'Y=mx+n
    sd.X1=Ref_dogru.X1
    sd.Y1=Ref_dogru.Y1
    Dim t As Float
    t=Aci_Bul(Kesen_dogru,Ref_dogru)
    'Sub Aci_Bul(line1 As Point,line2 As Point) As Float
    't =(m1-m2)/(1-m1*m2)
    'Return ((line1.m-line2.m)/(1+line1.m*line2.m))
    't-tm1m2=m1-m2
    't+m2=m1+tm1m2
    't+m2=m1(1+tm2)
    'm1=(t+m2)/(1+tm2) ******************
    sd.m=(t+Ref_dogru.m)/(1+t*Ref_dogru.m)
  
    'y=mx + n
    sd.n=sd.Y1-sd.m*sd.x1
    sd.X2=sd.X1-100
    sd.Y2=sd.m*sd.X2+sd.n
    Line_draw(sd,Colors.White,2)
    Return sd
End Sub
  


Sub Dik_doru_Ciz( dd_x As Float,dd_y As Float,Ref_dogru As Point) As Point
'***************dik doğruyu olşturm*************************************
    'dik doğrular için eğim m1 = -1/m2
    Dim dik_dogru As Point
  
    dik_dogru.X1=dd_x
    dik_dogru.Y1=dd_y
    dik_dogru.m=(-1/Ref_dogru.m)
    dik_dogru.n=dik_dogru.Y1-dik_dogru.m*dik_dogru.X1
  
    'Y= mx+n doğru forluüü
    dik_dogru.X2=dik_dogru.X1+50
    dik_dogru.Y2=dik_dogru.m*dik_dogru.X2+dik_dogru.n
    Line_draw(dik_dogru,Colors.Gray,2)
'***********************************************************  
    Return dik_dogru
  
End Sub

Sub Aci_Bul(line1 As Point,line2 As Point) As Float
    ' =(m1-m2)/(1-m1*m2)
    Return ((line1.m-line2.m)/(1+line1.m*line2.m))
End Sub

Sub lbl_LongClick
    lbl.Text="Clear all data"
    pnl.Color=Colors.Blue
    pnl.Invalidate
    a1=do_ZERO
    a2=do_ZERO
    a3=do_ZERO
    a4=do_ZERO
    c1=do_ZERO
    c2=do_ZERO
    c3=do_ZERO
    c4=do_ZERO
    d1=do_ZERO
    s1=do_ZERO
    ft =False
    sayac=0
  
End Sub

Sub Line_draw(Coordinat As Point,clr As Double,Wth As Int)
    Dim cnv As Canvas
    cnv.Initialize(pnl)
    cnv.DrawLine(Coordinat.X1,Coordinat.Y1,Coordinat.X2,Coordinat.Y2,clr, Wth) '5dip stoke width
    pnl.Invalidate
End Sub
 

Lahksman

Active Member
Licensed User
Longtime User
It's because of the next piece of code (starting at line 129).
B4X:
Select sayac
            Case 1
                a1=ff
                'Log("a1:"&a1)
                Line_draw(a1,Colors.red,3)
            Case 2
                a2=ff
                Line_draw(a2,Colors.red,3)
            Case 3
                a3=ff
                'Log("a3")
                Line_draw(a3,Colors.red,3)
            Case 4
                a4=ff
                'Log("a4")
                Line_draw(a4,Colors.red,3)
             
        End Select

You are setting a1 equal to ff and ff has the point info you are seeing in your log.
 
Upvote 0

Tayfur

Well-Known Member
Licensed User
Longtime User
It's because of the next piece of code (starting at line 129).
B4X:
Select sayac
            Case 1
                a1=ff
                'Log("a1:"&a1)
                Line_draw(a1,Colors.red,3)
            Case 2
                a2=ff
                Line_draw(a2,Colors.red,3)
            Case 3
                a3=ff
                'Log("a3")
                Line_draw(a3,Colors.red,3)
            Case 4
                a4=ff
                'Log("a4")
                Line_draw(a4,Colors.red,3)
            
        End Select

You are setting a1 equal to ff and ff has the point info you are seeing in your log.

hi;
I see,But;
I run debug mode and chek logs.
I run step by step. And I saw logs.
You can check it.

B4X:
Log("1.....>>:"&a1)
 yy.X2=X
 yy.Y2=Y
Log("2.....>>::"&a1)

I dont change a1 values.

Please look picture. (X2 values in point type)

Untitled.png
 
Upvote 0

Lahksman

Active Member
Licensed User
Longtime User
I see what's happening and it's very strange indeed.
Can't pinpoint it, but a1 get changed on touch, although I can't find why.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
I think that the problem is because you set somewhere yy = ff and also a1 = ff.
This means that all three variables point to the same object, as you change yy values between the two Logs you also change the a1 values. That's what you see.
 
Upvote 0

Tayfur

Well-Known Member
Licensed User
Longtime User
I think that the problem is because you set somewhere yy = ff and also a1 = ff.
This means that all three variables point to the same object, as you change yy values between the two Logs you also change the a1 values. That's what you see.
I change gloabal "yy" insteat of local varables.
Now works.

Thank you for informations @klaus
 
Upvote 0
Top