package com.dg360.mobile.ui.HomeScreen
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Path
import android.util.AttributeSet
import android.view.View
import java.lang.Math.*
class LineConnectingView(context: Context, attrs: AttributeSet?) : View(context, attrs) {
private val linePaint = Paint().apply {
color = Color.BLUE
arrowPath
strokeWidth = 5f
style = Paint.Style.STROKE
}
private val arrowSize = 30f
private val arrowPath = Path()
// List to store start and end coordinates of the lines
private val lineCoordinatesList = mutableListOf<Pair<Float, Float>>()
fun removelines(){
lineCoordinatesList.clear()
arrowPath.reset()
// linePaint.reset()
invalidate()
}
// Function to add start and end coordinates for a line
fun addLine(startX: Float, startY: Float, endX: Float, endY: Float) {
lineCoordinatesList.add(Pair(startX, startY))
lineCoordinatesList.add(Pair(endX, endY))
invalidate() // Request a redraw when new coordinates are added
}
// Override onDraw to draw the lines with arrowheads
override fun onDraw(canvas: Canvas) {
super.onDraw(canvas)
// Draw each line using the coordinates stored in the list
for (i in 0 until lineCoordinatesList.size step 2) {
val (startX, startY) = lineCoordinatesList[i]
val (endX, endY) = lineCoordinatesList[i + 1]
// Draw the line
canvas.drawLine(startX, startY, endX, endY, linePaint)
// Calculate the angle of the arrow
val angle = atan2((endY - startY).toDouble(), (endX - startX).toDouble()).toFloat()
// Draw the arrowhead
arrowPath.reset()
arrowPath.moveTo(endX, endY)
arrowPath.lineTo(
endX - arrowSize * cos(angle + toRadians(30.0)).toFloat(),
endY - arrowSize * sin(angle + toRadians(30.0)).toFloat()
)
arrowPath.lineTo(
endX - arrowSize * cos(angle - toRadians(30.0)).toFloat(),
endY - arrowSize * sin(angle - toRadians(30.0)).toFloat()
)
arrowPath.close()
canvas.drawPath(arrowPath, linePaint)
}
}
}