Coordinate systems for Draw and Path?

positrom2

Active Member
Licensed User
Longtime User
I was trying to understand employing "Path" to draw part of a circle (post #6 in:
http://www.b4x.com/forum/basic4android-updates-questions/8547-trigonometric-circle.html.
I "slightly" modified the code to additionally plot the points of the path using the "DrawPoint" function. The points are plotted far outside of the circle.
My question is, are the coordinate systems different in "Draw..." and "Path"? Or what is the reason for that behaviour?
B4X:
Sub Process_Globals

End Sub

Sub Globals
    Dim canvas1 As Canvas
End Sub

Sub Activity_Create(FirstTime As Boolean)
    canvas1.Initialize(Activity)
    DrawArc(canvas1, 100dip, 100dip, 100dip, 180, 90, Colors.Blue)
   
    Activity.Invalidate
End Sub

Sub DrawArc(cnvs As Canvas, x As Float, y As Float, radius As Float, startAngle As Float, endAngle As Float, Color As Int)
    
    Dim s As Float,x1,y1 As Int
    s = startAngle
    startAngle = 180 - endAngle
    endAngle = 180 - s
    If startAngle >= endAngle Then endAngle = endAngle + 360
    Dim p As Path
    p.Initialize(x, y)
    For i = startAngle To endAngle Step 10
   x1=x+2 * radius * SinD(i)
   y1=y+2 * radius * CosD(i)
   cnvs.DrawPoint(x1,y1,Colors.yellow)
   Log(i&" "&x1&" "&y1)
       p.LineTo(x1 ,  y1)
    Next
    p.LineTo(x + 2 * radius * SinD(endAngle), y + 2 * radius * CosD(endAngle))
    p.LineTo(x, y)
   cnvs.ClipPath(p) 'We are limiting the drawings to the required slice
   cnvs.DrawCircle(x, y, .5*radius, Colors.red, False, 0)
   cnvs.RemoveClip
End Sub

Thanks for an answer, positrom2
 

Informatix

Expert
Licensed User
Longtime User
I was trying to understand employing "Path" to draw part of a circle (post #6 in:
http://www.b4x.com/forum/basic4android-updates-questions/8547-trigonometric-circle.html.
I "slightly" modified the code to additionally plot the points of the path using the "DrawPoint" function. The points are plotted far outside of the circle.
My question is, are the coordinate systems different in "Draw..." and "Path"? Or what is the reason for that behaviour?
...

Thanks for an answer, positrom2

Don't waste your time with that ! Use my Accelerated Surface lib for your plottings. You'll get more functions for the Canvas and Path classes, and you'll benefit from the hardware acceleration.
 
Upvote 0

positrom2

Active Member
Licensed User
Longtime User
Update, solved:
I see the reason: In order to see the points of the path segments, I had drawn the circle with 0.5*radius (3rd line from bottom)...Sorry, forgot about that.
Regards, positrom2
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
The scale is the same !
The problem comes from here:
B4X:
x1 = x + 2 * radius * SinD(i)
y1 = y + 2 * radius * CosD(i)
For the circle you would use
B4X:
x1 = x + radius * SinD(i)
y1 = y + radius * CosD(i)
That means that the path is double as wide as the circle.

Best regards.
 
Upvote 0

positrom2

Active Member
Licensed User
Longtime User
Klaus, thanks. During my nap at noon I was startled by realizing that I made that mistake...
But, anyway, is there a simple way to watch the path?
Thanks, positrom2
 
Upvote 0

positrom2

Active Member
Licensed User
Longtime User
Thanks, Klaus. But I still have a question. In the example code above the path is simply drawn by
B4X:
p.Initialize(x, y)
..
p.LineTo(x1 ,  y1)
without reference to a Panel or canvas.
If I have different Panels (Canvases) how can I specify where the path shall go?
Regards, positrom2
 
Upvote 0

positrom2

Active Member
Licensed User
Longtime User
Still being unsatisfied...
The Path seems not be referred to a canvas (so the scales must be different).

A result of my experiments is that canvasXY.drawpath draws the same on the screen regardless of the specific canvas size and position.
Is that right?

Regards, positrom2
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
As already said, the path is defined by coordinates and has no relationship to any canvas.
A path is a kind of array of lines defined by the end point coordiantes of the lines expressed in pixels.
You can define different canvases and draw the path with each canvas.

A result of my experiments is that canvasXY.drawpath draws the same on the screen regardless of the specific canvas size and position.
How did you define canvasXY?
... regardless of the specific canvas size and position
What do you mean with canvas size and canvas position ?
Each canvas has a dedicated target view background or a bitmap to draw on.

Best regards.
 
Upvote 0

Informatix

Expert
Licensed User
Longtime User
I don't remember how the path is related to the canvas in the standard canvas. I will just tell how it is implemented in my AS lib (because that's how it should be):
When you draw a Path, the coordinates of its points are added to the origin of the canvas. If your path's first point is at 20, 5 and you draw this path on the canvas at 0, 0, the first point will be at 20,5. If you draw the same path at 10, 20, the first point will be at 30,25.
 
Upvote 0

positrom2

Active Member
Licensed User
Longtime User
Thanks to both (Informatix and Klaus).
The path coordinates are refered to the upper left corner of the canvas bitmap.
However, more clearly (see Informatix):
"The path coordinates are added to the coordinates of the upper left corner of the canvas bitmap."
Would be nice if that information were included in the documentation.
Regards, positrom2
 
Upvote 0
Top