11package com.smarttoolfactory.tutorial1_1basics.chapter6_graphics
22
3+ import androidx.compose.ui.geometry.Offset
34import androidx.compose.ui.geometry.Rect
45import androidx.compose.ui.geometry.Size
56import androidx.compose.ui.graphics.Path
@@ -39,133 +40,135 @@ fun createPolygonPath(cx: Float, cy: Float, sides: Int, radius: Float): Path {
3940 }
4041}
4142
42- fun roundedRectanglePath (size : Size , cornerRadius : Float ): Path {
43+ fun roundedRectanglePath (topLeft : Offset = Offset . Zero , size : Size , cornerRadius : Float ): Path {
4344 return Path ().apply {
4445 reset()
4546
4647 // Top left arc
4748 val radius = cornerRadius * 2
4849 arcTo(
4950 rect = Rect (
50- left = 0f ,
51- top = 0f ,
52- right = radius,
53- bottom = radius
51+ left = topLeft.x ,
52+ top = topLeft.y ,
53+ right = topLeft.x + radius,
54+ bottom = topLeft.y + radius
5455 ),
5556 startAngleDegrees = 180.0f ,
5657 sweepAngleDegrees = 90.0f ,
5758 forceMoveTo = false
5859 )
5960
60- lineTo(x = size.width - radius, y = 0f )
61+ lineTo(x = topLeft.x + size.width - radius, y = topLeft.y )
6162
6263 // Top right arc
6364 arcTo(
6465 rect = Rect (
65- left = size.width - radius,
66- top = 0f ,
67- right = size.width,
68- bottom = radius
66+ left = topLeft.x + size.width - radius,
67+ top = topLeft.y ,
68+ right = topLeft.x + size.width,
69+ bottom = topLeft.y + radius
6970 ),
7071 startAngleDegrees = - 90.0f ,
7172 sweepAngleDegrees = 90.0f ,
7273 forceMoveTo = false
7374 )
7475
75- lineTo(x = size.width, y = size.height - radius)
76+ lineTo(x = topLeft.x + size.width, y = topLeft.y + size.height - radius)
7677
7778 // Bottom right arc
7879 arcTo(
7980 rect = Rect (
80- left = size.width - radius,
81- top = size.height - radius,
82- right = size.width ,
83- bottom = size.height
81+ left = topLeft.x + size.width - radius,
82+ top = topLeft.y + size.height - radius,
83+ right = topLeft.x + size.width,
84+ bottom = topLeft.y + size.height
8485 ),
8586 startAngleDegrees = 0f ,
8687 sweepAngleDegrees = 90.0f ,
8788 forceMoveTo = false
8889 )
8990
90- lineTo(x = radius, y = size.height)
91+ lineTo(x = topLeft.x + radius, y = topLeft.y + size.height)
9192
9293 // Bottom left arc
9394 arcTo(
9495 rect = Rect (
95- left = 0f ,
96- top = size.height - radius,
97- right = radius,
98- bottom = size.height
96+ left = topLeft.x ,
97+ top = topLeft.y + size.height - radius,
98+ right = topLeft.x + radius,
99+ bottom = topLeft.y + size.height
99100 ),
100101 startAngleDegrees = 90.0f ,
101102 sweepAngleDegrees = 90.0f ,
102103 forceMoveTo = false
103104 )
104105
105- lineTo(x = 0f , y = radius)
106+ lineTo(x = topLeft.x , y = topLeft.y + radius)
106107 close()
107108 }
108109}
109110
110111/* *
112+ * Create a ticket path with given size and corner radius in px with offset [topLeft].
111113 *
112- *
114+ * Refer [this link](https://juliensalvi.medium.com/custom-shape-with-jetpack-compose-1cb48a991d42)
115+ * for implementation details.
113116 */
114- fun ticketPath (size : Size , cornerRadius : Float ): Path {
117+ fun ticketPath (topLeft : Offset = Offset . Zero , size : Size , cornerRadius : Float ): Path {
115118 return Path ().apply {
116119 reset()
117120 // Top left arc
118121 arcTo(
119122 rect = Rect (
120- left = - cornerRadius,
121- top = - cornerRadius,
122- right = cornerRadius,
123- bottom = cornerRadius
123+ left = topLeft.x + - cornerRadius,
124+ top = topLeft.y + - cornerRadius,
125+ right = topLeft.x + cornerRadius,
126+ bottom = topLeft.y + cornerRadius
124127 ),
125128 startAngleDegrees = 90.0f ,
126129 sweepAngleDegrees = - 90.0f ,
127130 forceMoveTo = false
128131 )
129- lineTo(x = size.width - cornerRadius, y = 0f )
132+ lineTo(x = topLeft.x + size.width - cornerRadius, y = topLeft.y )
130133 // Top right arc
131134 arcTo(
132135 rect = Rect (
133- left = size.width - cornerRadius,
134- top = - cornerRadius,
135- right = size.width + cornerRadius,
136- bottom = cornerRadius
136+ left = topLeft.x + size.width - cornerRadius,
137+ top = topLeft.y + - cornerRadius,
138+ right = topLeft.x + size.width + cornerRadius,
139+ bottom = topLeft.y + cornerRadius
137140 ),
138141 startAngleDegrees = 180.0f ,
139142 sweepAngleDegrees = - 90.0f ,
140143 forceMoveTo = false
141144 )
142- lineTo(x = size.width, y = size.height - cornerRadius)
145+ lineTo(x = topLeft.x + size.width, y = topLeft.y + size.height - cornerRadius)
143146 // Bottom right arc
144147 arcTo(
145148 rect = Rect (
146- left = size.width - cornerRadius,
147- top = size.height - cornerRadius,
148- right = size.width + cornerRadius,
149- bottom = size.height + cornerRadius
149+ left = topLeft.x + size.width - cornerRadius,
150+ top = topLeft.y + size.height - cornerRadius,
151+ right = topLeft.x + size.width + cornerRadius,
152+ bottom = topLeft.y + size.height + cornerRadius
150153 ),
151154 startAngleDegrees = 270.0f ,
152155 sweepAngleDegrees = - 90.0f ,
153156 forceMoveTo = false
154157 )
155- lineTo(x = cornerRadius, y = size.height)
158+ lineTo(x = topLeft.x + cornerRadius, y = topLeft.y + size.height)
156159 // Bottom left arc
157160 arcTo(
158161 rect = Rect (
159- left = - cornerRadius,
160- top = size.height - cornerRadius,
161- right = cornerRadius,
162- bottom = size.height + cornerRadius
162+ left = topLeft.x + - cornerRadius,
163+ top = topLeft.y + size.height - cornerRadius,
164+ right = topLeft.x + cornerRadius,
165+ bottom = topLeft.y + size.height + cornerRadius
163166 ),
164167 startAngleDegrees = 0.0f ,
165168 sweepAngleDegrees = - 90.0f ,
166169 forceMoveTo = false
167170 )
168- lineTo(x = 0f , y = cornerRadius)
171+ lineTo(x = topLeft.x , y = topLeft.y + cornerRadius)
169172 close()
170173 }
171174}
0 commit comments