mirror of
https://github.com/dzeiocom/OpenHealth.git
synced 2025-04-23 19:32:11 +00:00
fix: Add missing utils file
This commit is contained in:
parent
4a9d4b8ede
commit
6fa96eeb87
49
charts/src/main/java/com/dzeio/charts/utils/CanvasUtils.kt
Normal file
49
charts/src/main/java/com/dzeio/charts/utils/CanvasUtils.kt
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
package com.dzeio.charts.utils
|
||||||
|
|
||||||
|
import android.graphics.Canvas
|
||||||
|
import android.graphics.Paint
|
||||||
|
import kotlin.math.sqrt
|
||||||
|
|
||||||
|
fun Canvas.drawDottedLine(
|
||||||
|
startX: Float,
|
||||||
|
startY: Float,
|
||||||
|
endX: Float,
|
||||||
|
endY: Float,
|
||||||
|
spacing: Float,
|
||||||
|
paint: Paint
|
||||||
|
) {
|
||||||
|
//calculate line length
|
||||||
|
val length = if (endX - startX == 0f) {
|
||||||
|
// just length of Y
|
||||||
|
endY - startY
|
||||||
|
} else if (endY - startY == 0f) {
|
||||||
|
// just length of X
|
||||||
|
endX - startX
|
||||||
|
} else {
|
||||||
|
// calculate using the Pythagorean theorem
|
||||||
|
sqrt((startX + endX) * (startX + endX) + (startY + endY) * (startY + endY))
|
||||||
|
}
|
||||||
|
|
||||||
|
val lineCount = (length / spacing).toInt()
|
||||||
|
|
||||||
|
val lenX = endX - startX
|
||||||
|
val lenY = endY - startY
|
||||||
|
|
||||||
|
// Log.d("DrawDottedLine", "----------- Start -----------")
|
||||||
|
// Log.d("DrawDottedLine", "lenX: $lenX, lenY: $lenY")
|
||||||
|
for (line in 0 until lineCount) {
|
||||||
|
if (line % 2 == 0) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
val sx = lenX / lineCount * line + startX
|
||||||
|
val sy = lenY / lineCount * line + startY
|
||||||
|
val ex = lenX / lineCount * (line + 1) + startX
|
||||||
|
val ey = lenY / lineCount * (line + 1) + startY
|
||||||
|
// Log.d("DrawDottedLine", "$sx, $sy, $ex, $ey")
|
||||||
|
this.drawLine(sx, sy, ex, ey, paint)
|
||||||
|
// line
|
||||||
|
// total line startX, endX, startY, endY
|
||||||
|
// total line length
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user