@@ -295,31 +295,83 @@ class DynamicLineTool(
295
295
}
296
296
297
297
private fun updateMovingGhostVertices (coordinate : PointF ) {
298
+ var movingVertexCenter: PointF = getMovingVertexCenter(coordinate)
298
299
if (movingVertex != null ) {
299
- movingVertex?.updateVertexCenter(copyPointF(coordinate ))
300
+ movingVertex?.updateVertexCenter(copyPointF(movingVertexCenter ))
300
301
if (movingVertex?.ingoingPathCommand != null ) {
301
302
ingoingStartCoordinate = predecessorVertex?.vertexCenter?.let { center -> copyPointF(center) }
302
- ingoingEndCoordinate = copyPointF(coordinate )
303
+ ingoingEndCoordinate = copyPointF(movingVertexCenter )
303
304
ingoingGhostPathPaint = createGhostPathPaint(movingVertex?.ingoingPathCommand?.paint)
304
305
}
305
306
if (movingVertex?.outgoingPathCommand != null ) {
306
- outgoingStartCoordinate = copyPointF(coordinate )
307
+ outgoingStartCoordinate = copyPointF(movingVertexCenter )
307
308
outgoingEndCoordinate = successorVertex?.vertexCenter?.let { center -> copyPointF(center) }
308
309
outgoingGhostPathPaint = createGhostPathPaint(movingVertex?.outgoingPathCommand?.paint)
309
310
}
310
311
}
311
312
}
312
313
314
+ private fun getMovingVertexCenter (coordinate : PointF ): PointF {
315
+ var insidePoint: PointF ? = getInsidePoint()
316
+ var outsidePoint = copyPointF(coordinate)
317
+ return calculateMovingVertexCenter(insidePoint, outsidePoint)
318
+ }
319
+
320
+ private fun calculateMovingVertexCenter (insidePoint : PointF ? , outsidePoint : PointF ): PointF {
321
+ if (insidePoint == null ) return outsidePoint
322
+
323
+ var slope = (outsidePoint.y - insidePoint.y) / (outsidePoint.x - insidePoint.x)
324
+ val yIntercept = insidePoint.y - slope * insidePoint.x
325
+ val surfaceHeight = workspace.height.toFloat()
326
+ val surfaceWidth = workspace.width.toFloat()
327
+
328
+ if (outsidePoint.y < 0 ) {
329
+ val x = - yIntercept / slope
330
+ if (x in 0.0f .. surfaceWidth) {
331
+ return PointF (x, 0f )
332
+ }
333
+ }
334
+
335
+ if (outsidePoint.y > surfaceHeight) {
336
+ val x = (surfaceHeight - yIntercept) / slope
337
+ if (x in 0.0f .. surfaceWidth) {
338
+ return PointF (x, surfaceHeight)
339
+ }
340
+ }
341
+
342
+ if (outsidePoint.x < 0 && yIntercept in 0.0f .. surfaceHeight) {
343
+ return PointF (0f , yIntercept)
344
+ }
345
+
346
+ if (outsidePoint.x > surfaceWidth) {
347
+ val y = slope * surfaceWidth + yIntercept
348
+ if (y in 0.0f .. surfaceHeight) {
349
+ return PointF (surfaceWidth, y)
350
+ }
351
+ }
352
+
353
+ return outsidePoint
354
+ }
355
+
356
+ private fun getInsidePoint (): PointF ? {
357
+ return if (predecessorVertex != null ) {
358
+ predecessorVertex?.vertexCenter?.let { copyPointF(it) }
359
+ } else {
360
+ successorVertex?.vertexCenter?.let { copyPointF(it) }
361
+ }
362
+ }
363
+
313
364
private fun updateMovingVertices (coordinate : PointF ) {
365
+ var movingVertexCenter = getMovingVertexCenter(coordinate)
314
366
if (movingVertex != null ) {
315
- movingVertex?.updateVertexCenter(copyPointF(coordinate ))
367
+ movingVertex?.updateVertexCenter(copyPointF(movingVertexCenter ))
316
368
if (movingVertex?.ingoingPathCommand != null ) {
317
369
val startPoint = predecessorVertex?.vertexCenter?.let { center -> copyPointF(center) }
318
- val endPoint = copyPointF(coordinate )
370
+ val endPoint = copyPointF(movingVertexCenter )
319
371
updatePathCommand(startPoint, endPoint, movingVertex?.ingoingPathCommand)
320
372
}
321
373
if (movingVertex?.outgoingPathCommand != null ) {
322
- val startPoint = copyPointF(coordinate )
374
+ val startPoint = copyPointF(movingVertexCenter )
323
375
val endPoint = successorVertex?.vertexCenter?.let { center -> copyPointF(center) }
324
376
updatePathCommand(startPoint, endPoint, movingVertex?.outgoingPathCommand)
325
377
}
0 commit comments