@@ -22,6 +22,7 @@ import javafx.scene.shape.FillRule
22
22
import javafx.scene.shape.StrokeLineCap
23
23
import javafx.scene.shape.StrokeLineJoin
24
24
import javafx.stage.Stage
25
+ import kotlin.math.abs
25
26
import kotlin.math.roundToInt
26
27
27
28
typealias JFXColor = javafx.scene.paint.Color
@@ -400,9 +401,9 @@ class ImplJFX(private val stage: Stage, private var canvas: Canvas) {
400
401
val isBary = vtx1.col != vtx2.col || vtx2.col != vtx3.col
401
402
val doBary = if (isBary) {
402
403
triangleArea(vtx1.pos, vtx2.pos, vtx3.pos) >= BARYCENTRIC_SIZE_THRESHOLD &&
403
- atLeastTwo(Math . abs(vtx1.pos.x - vtx2.pos.x) >= 2.0 ,
404
- Math . abs(vtx1.pos.x - vtx3.pos.x) >= 2.0 ,
405
- Math . abs(vtx3.pos.x - vtx2.pos.x) >= 2.0 )
404
+ atLeastTwo(abs(vtx1.pos.x - vtx2.pos.x) >= 2.0 ,
405
+ abs(vtx1.pos.x - vtx3.pos.x) >= 2.0 ,
406
+ abs(vtx3.pos.x - vtx2.pos.x) >= 2.0 )
406
407
} else {
407
408
false
408
409
}
@@ -492,8 +493,8 @@ class ImplJFX(private val stage: Stage, private var canvas: Canvas) {
492
493
493
494
// if it's a line, needs to be drawn specially so that only 1 pixel isn't drawn
494
495
if (maxY - minY > 1.0f ) {
495
- for (scanlineY in Math .round(minY).i .. Math .round(maxY).i ) {
496
- for (x in Math .round(curx1).i .. Math .round(curx2).i ) {
496
+ for (scanlineY in minY.roundToInt() .. maxY.roundToInt() ) {
497
+ for (x in curx1.roundToInt() .. curx2.roundToInt() ) {
497
498
baryColor(Vec2 (x, scanlineY))
498
499
}
499
500
curx1 + = invslope1
@@ -504,7 +505,7 @@ class ImplJFX(private val stage: Stage, private var canvas: Canvas) {
504
505
} else {
505
506
// average where the line goes
506
507
val scanlineY = (maxY + minY) / 2.0f
507
- for (x in Math .round(minX).i .. Math .round(maxX).i ) {
508
+ for (x in minX.roundToInt() .. maxX.roundToInt() ) {
508
509
// draw the color at each point on the line
509
510
baryColor(Vec2 (x, scanlineY))
510
511
}
@@ -532,8 +533,8 @@ class ImplJFX(private val stage: Stage, private var canvas: Canvas) {
532
533
val maxX = vs3.x max vs2.x max vs1.y
533
534
534
535
if (maxY - minY > 1.0f ) {
535
- for (scanlineY in Math .round(maxY).i downTo Math .round(minY).i ) {
536
- for (x in Math .round(curx1).i .. Math .round(curx2).i ) {
536
+ for (scanlineY in maxY.roundToInt() downTo minY.roundToInt() ) {
537
+ for (x in curx1.roundToInt() .. curx2.roundToInt() ) {
537
538
baryColor(Vec2 (x, scanlineY))
538
539
}
539
540
curx1 - = invslope1
@@ -543,7 +544,7 @@ class ImplJFX(private val stage: Stage, private var canvas: Canvas) {
543
544
}
544
545
} else {
545
546
val scanlineY = (maxY + minY) / 2.0f
546
- for (x in Math .round(minX).i .. Math .round(maxX).i ) {
547
+ for (x in minX.roundToInt() .. maxX.roundToInt() ) {
547
548
baryColor(Vec2 (x, scanlineY))
548
549
}
549
550
}
@@ -594,12 +595,36 @@ class ImplJFX(private val stage: Stage, private var canvas: Canvas) {
594
595
(((col1 ushr COL32_B_SHIFT ) and COLOR_SIZE_MASK ) * color.blue).i,
595
596
(((col1 ushr COL32_A_SHIFT ) and COLOR_SIZE_MASK ) / COLOR_SIZE_MASK .toDouble()) * color.opacity)
596
597
// add initial points
597
- addPoint(vtx1.pos.x, vtx1.pos.y)
598
- addPoint(vtx2.pos.x, vtx2.pos.y)
599
- addPoint(vtx3.pos.x, vtx3.pos.y)
598
+ if (pos + 2 >= xs.size) {
599
+ if (DEBUG )
600
+ println (" increase points buffer size (old ${xs.size} , new ${xs.size * 2 } )" )
601
+ val nx = DoubleArray (xs.size * 2 )
602
+ val ny = DoubleArray (ys.size * 2 )
603
+ xs.copyInto(nx)
604
+ ys.copyInto(ny)
605
+ xs = nx
606
+ ys = ny
607
+ }
608
+ xs[pos] = vtx1.pos.x.d
609
+ ys[pos++ ] = vtx1.pos.y.d
610
+ xs[pos] = vtx2.pos.x.d
611
+ ys[pos++ ] = vtx2.pos.y.d
612
+ xs[pos] = vtx3.pos.x.d
613
+ ys[pos++ ] = vtx3.pos.y.d
600
614
}
601
615
// add bordering point
602
- addPoint(vtx6.pos.x, vtx6.pos.y)
616
+ if (pos == xs.size) {
617
+ if (DEBUG )
618
+ println (" increase points buffer size (old ${xs.size} , new ${xs.size * 2 } )" )
619
+ val nx = DoubleArray (xs.size * 2 )
620
+ val ny = DoubleArray (ys.size * 2 )
621
+ xs.copyInto(nx)
622
+ ys.copyInto(ny)
623
+ xs = nx
624
+ ys = ny
625
+ }
626
+ xs[pos] = vtx6.pos.x.d
627
+ ys[pos++ ] = vtx6.pos.y.d
603
628
} else {
604
629
// does not border the next triangle , do either draw this triangle or draw the last one
605
630
// if the last triangle bordered this one, the vertex is already in the shape, so just draw
@@ -755,7 +780,7 @@ fun Int.toVec4(): Vec4 {
755
780
756
781
inline fun dotProd (a : Vec2 , b : Vec2 ) = ((a.x * b.x) + (a.y * b.y)).d
757
782
758
- inline fun triangleArea (a : Vec2 , b : Vec2 , c : Vec2 ) = Math . abs((a.x * (b.y - c.y)) + (b.x * (c.y - a.y)) + (c.x * (a.y - b.y)))
783
+ inline fun triangleArea (a : Vec2 , b : Vec2 , c : Vec2 ) = abs((a.x * (b.y - c.y)) + (b.x * (c.y - a.y)) + (c.x * (a.y - b.y)))
759
784
760
785
inline fun atLeastTwo (a : Boolean , b : Boolean , c : Boolean ): Boolean {
761
786
return if (a) b || c else b && c
0 commit comments