2016-11-28 5 views
0

ax + by> = c(1)とx> = zおよびy> = k(2)のいくつかの制限タイプがあります。制限(1)と(2)は地域を作る。私はこの地域の交差点を見つけて、他の色を塗りつぶす必要があります。 JavaFXでどうすればいいですか? Canvasを使ってこれを解決することはできますか?JavaFXの2つの領域の交点を引く

Example

+0

...しかし、あなたにポリゴンのコーナーポイントを与えることはありません。将来的にはhttp://stackoverflow.com/help/mcveおよびhttp://stackoverflow.com/help/how-to-askを参考にしてください。 –

答えて

0

確かに、ちょうどポリゴンの色ですべてを記入し、各エリアの背景色を持つエリアにいないすべてのものを埋めます。

Canvasの場合は、背景色のポリゴンを除いて全体を塗りつぶすことに注意してください。

public class Area { 

    private final double x; 
    private final double y; 

    private final double threshold; 
    private final int minCorner; 

    public Area(double x, double y, double threshold, boolean greater) { 
     if (x == 0 && y == 0) { 
      throw new IllegalArgumentException(); 
     } 

     if (greater) { 
      x *= -1; 
      y *= -1; 
      threshold *= -1; 
     } 
     this.x = x; 
     this.y = y; 
     this.threshold = threshold; 
     boolean yPos = y > 0; 

     // find corner with minimum result for evaluate 
     this.minCorner = x < 0 ? (yPos ? 1 : 2) : (yPos ? 0 : 3); 
    } 

    public Area(double x, double y, double threshold) { 
     this(x, y, threshold, false); 
    } 

    private static final int[][] CORNER_FACTORS = { 
     {0, 0}, 
     {1, 0}, 
     {1, 1}, 
     {0, 1} 
    }; 

    public boolean contains(double x, double y) { 
     return evaluate(x, y) <= threshold; 
    } 

    public double hLineIntersection(double y) { 
     if (x == 0) { 
      return this.y * y == threshold ? Double.POSITIVE_INFINITY : Double.NaN; 
     } else { 
      return (threshold - this.y * y)/this.x; 
     } 
    } 

    public double vLineIntersection(double x) { 
     if (y == 0) { 
      return this.x * x == threshold ? Double.POSITIVE_INFINITY : Double.NaN; 
     } else { 
      return (threshold - this.x * x)/this.y; 
     } 
    } 

    private double evaluate(double x, double y) { 
     return this.x * x + this.y * y; 
    } 

    public void fillCleanArea(GraphicsContext gc, double w, double h) { 
     double[] xcoords = new double[5]; 
     double[] ycoords = new double[5]; 

     int[] factors = CORNER_FACTORS[minCorner]; 
     boolean inside = contains(factors[0] * w, factors[1] * h); 
     int ptIndex = 0; 

     for (int i = minCorner, max = minCorner + 4; i < max; i++) { 
      factors = CORNER_FACTORS[i % 4]; 
      double x = factors[0] * w; 
      double y = factors[1] * h; 
      boolean nowInside = contains(x, y); 
      if (inside != nowInside) { 
       // add intersection point with side 
       if ((i & 1) == 0) { 
        ycoords[ptIndex] = vLineIntersection(x); 
        xcoords[ptIndex++] = x; 
       } else { 
        xcoords[ptIndex] = hLineIntersection(y); 
        ycoords[ptIndex++] = y; 
       } 

       inside = nowInside; 

       // stop, if the end point is inside the area again 
       if (inside) { 
        break; 
       } 
      } 

      // add corners outside the bounds to polygon 
      if (!inside) { 
       xcoords[ptIndex] = x; 
       ycoords[ptIndex++] = y; 
      } 
     } 

     // draw polygon 
     if (ptIndex > 0) { 
      gc.fillPolygon(xcoords, ycoords, ptIndex); 
     } 
    } 

} 
public static void draw(Canvas canvas, Paint fill, Paint background, Area... areas) { 
    GraphicsContext gc = canvas.getGraphicsContext2D(); 
    double w = canvas.getWidth(); 
    double h = canvas.getHeight(); 

    // fill everything with polygon color 
    gc.setFill(fill); 
    gc.fillRect(0, 0, w, h); 

    // fill everything outside the polygon with background color 
    gc.setFill(background); 
    for (Area area : areas) { 
     area.fillCleanArea(gc, w, h); 
    } 
} 

@Override 
public void start(Stage primaryStage) { 
    Canvas canvas = new Canvas(400, 400); 

    draw(canvas, Color.BLUE, Color.WHITE, 
      new Area(1, 1, 400), new Area(1, -1, 100), new Area(1, -1, -100, true), new Area(1, 2, 250, true)); 

    StackPane root = new StackPane(canvas); 

    Scene scene = new Scene(root); 

    primaryStage.setScene(scene); 
    primaryStage.show(); 
} 

これは私たちにあなたの努力をご提示ください

関連する問題