Scanline algorithm: how to calculate intersection points - java

I have to implement a scanline algorithm from our professor but I don't really understand how I get the intersection points from the scanline with a polygon.
Here is the algorithm:
I implemented my own polygon(with methods like paint(), contains() and so on) already and I have all edges from the polygon saved in an array like this:
int[] pointsX;
int[] pointsY;
and I have the min and max values for x and y saved in
int ymin, ymax, xmin, xmax;
So my first thought is that I have to create a scanline starting from 0,ymin and check in a loop if the next point is inside the polygon. I implemented this method like this:
public boolean contains(Point test) {
boolean result = false;
java.awt.Polygon polygon = new java.awt.Polygon(pointsX, pointsY, pointsX.length);
if (polygon.contains(test)) {
result = true;
}
return result;
}
So when the next point is inside the polygon, I have a intersection point and so on. For this i have this loop:
ArrayList<Point> intersectionPoints = new ArrayList<>();
wasInside = false;
for (int yTemp = ymin; yTemp <= ymax; yTemp++) {
for (int xTemp = xmin; xTemp <= xmax; xTemp++) {
if (wasInside != this.contains(new Point(xTemp, yTemp))) {
intersectionPoints.add(new Point(xTemp, yTemp));
wasInside = !wasInside;
}
}
}
But I got a hint that this is no proper solution from my stackoverflow question.
Can someone give me a hint, how I can start implementing the algorithm from my professor? Where do I get the x1,y1,x2,y2,c points? I know that these are the edges but how do I know which edges do I have to take?
EDIT:
OK, now I have all Edges sorted by their y values. Can I calculate the intersection points with the given formula Sx=x1+(x2-x1)/...?
My first try looks like this:
for (int c = ymin; c <= ymax; c++) {
for (int xTemp = xmin; xTemp <= xmax; xTemp++) {
for (int currentEdge = 0; currentEdge < edges.size() - 1; currentEdge++) {
int x1 = edges.get(currentEdge).x;
int x2 = edges.get(currentEdge + 1).x;
int y1 = edges.get(currentEdge).y;
int y2 = edges.get(currentEdge + 1).y;
if ((y1 <= c && y2 > c) || (y2 <= c && y1 > c)) {
intersectionPoints.add(new Point((x1 + (x2 - x1) / (y2 - y1) * (c - y1)),c));
}
}
}
}
But this seems to be wrong, because I get a lot of wrong Points in intersectionPoints.

The problem was that I calculated with int numbers and an int divided by another int results in inaccuracies. So just doing this with double numbers solved it.
This is how I calculate the intersection points:
edges is a ArrayList<Point> containing the edge points.
ymin is the lowest y value and `ymax`` the highest one.
for (int yTemp = ymin; yTemp <= ymax; yTemp++) {
ArrayList<Point> intersectionPoints = new ArrayList<>();
for (int p = 0; p < edges.size() - 1; p++) {
int x1, x2, y1, y2;
double deltax, deltay, x;
x1 = edges.get(p).x;
y1 = edges.get(p).y;
x2 = edges.get(p + 1).x;
y2 = edges.get(p + 1).y;
deltax = x2 - x1;
deltay = y2 - y1;
int roundedX;
x = x1 + deltax / deltay * (yTemp - y1);
roundedX = (int) Math.round(x);
if ((y1 <= yTemp && y2 > yTemp) || (y2 <= yTemp && y1 > yTemp)) {
intersectionPoints.add(new Point(roundedX, yTemp));
}
}
//for the last interval
int x1, x2, y1, y2;
x1 = edges.get(edges.size() - 1).x;
y1 = edges.get(edges.size() - 1).y;
x2 = edges.get(0).x;
y2 = edges.get(0).y;
if ((y1 <= yTemp && y2 > yTemp) || (y2 <= yTemp && y1 > yTemp)) {
intersectionPoints.add(new Point(x1 + (x2 - x1) / (y2 - y1) * yTemp - y1, yTemp));
}
//you have to sort the intersection points of every scanline from the lowest x value to thr highest
Collections.sort(intersectionPoints, new SortXPoints());
pointsOfScanline.add(intersectionPoints);
This will give you an ArrayList containing ArrayLists of scanline points for every Scanline. So you just have to draw them with drawLine(x1, y2, x2, y2).

Related

Find the normal of the intersection between a polygon and a line libgdx

I have modified some code in the Intersector class in libgdx to find the intersection between a line and a polygon. However, I am unsure how to calculate the normal of the point of collision. Below is some code that I have.
public static Vector2 intersectSegmentPolygon (Vector2 p1, Vector2 p2, Polygon polygon) {
float[] vertices = polygon.getTransformedVertices();
float x1 = p1.x, y1 = p1.y, x2 = p2.x, y2 = p2.y;
int n = vertices.length;
float x3 = vertices[n - 2], y3 = vertices[n - 1];
for (int i = 0; i < n; i += 2) {
float x4 = vertices[i], y4 = vertices[i + 1];
float d = (y4 - y3) * (x2 - x1) - (x4 - x3) * (y2 - y1);
if (d != 0) {
float yd = y1 - y3;
float xd = x1 - x3;
float ua = ((x4 - x3) * yd - (y4 - y3) * xd) / d;
if (ua >= 0 && ua <= 1) {
float ub = ((x2 - x1) * yd - (y2 - y1) * xd) / d;
if (ub >= 0 && ub <= 1) {
return new Vector2(x1 + (x2 - x1) * ua, y1 + (y2 - y1) * ua);
}
}
}
x3 = x4;
y3 = y4;
}
return null;
}

Coordinate values arent stored in processing

I really have no idea why the x and y values wont go to the drawLines function
float x, x1, x2;
float y, y1, y2;
float rad; //radius
int lines = 30; //number of lines
int colorNumber = 1;
void setup() {
background(#FFFFFF);
size (800, 600);
rad = 8;
}
void draw() {
}
This creates the three dots or vertices of the mathematical envelope
void mouseClicked() {
float x = mouseX;
float x1 = mouseX;
float x2 = mouseX;
float y = mouseY;
float y1 = mouseY;
float y2 = mouseY;
if (colorNumber == 1) {
fill(#9393ff);
ellipse(x, y, rad, rad);
} else if (colorNumber == 2) {
fill(#FF9393);
ellipse(x1, y1, rad, rad);
} else if (colorNumber == 3) {
fill(#93ff93);
ellipse(x2, y2, rad, rad);
}
}
This is supposed to draw the envelope using the coordinates of the vertices
void drawLines(int numLines) {
for (int i = 0; i < numLines; i = i + 1) {
float x = mouseX;
float x1 = mouseX;
float x2 = mouseX;
float y = mouseY;
float y1 = mouseY;
float y2 = mouseY;
float t = (float) i/(numLines-1);
float startX = x + t * (x1 - x);
float startY = y + t * (y1 - y);
float endX = x1 + t * (x2 - x1);
float endY = y1 + t * (y2 - y1);
line (startX, startY, endX, endY);
}
}
void mouseReleased() {
colorNumber++;
if (colorNumber == 4) {
colorNumber = 1;
}
println(colorNumber);
}
void keyPressed() {
if (keyPressed == true) {
background(#FFFFFF);
}
}
this last stuff just tells the code if you press a key, it will reset the backround
I understand your intention with using mouseX and mouseY to specify the coordinates of one of the 3 points of the envelope on click. The current issue is that all 3 points are being set to the same coordinate with each click. You need to introduce a variable to keep track of which coordinate to set on-click, such that only one pair is set. Then, only once all 3 coordinates are set, drawLines() can be called.
I propose the following:
Introduce 2 variables, one to keep track of which point is being modified; the other an array of PVectors (just to make it cleaner).
int index = 0;
PVector[] coords = new PVector[3];
Modify mouseClicked() to include the following:
void mouseClicked() {
ellipse(mouseX, mouseY, 8, 8);
coords[index] = new PVector(mouseX, mouseY);
index += 1;
if (index == 3) {
drawLines(lines);
}
index %= 3;
}
drawLines() becomes:
void drawLines(int numLines) {
for (int i = 0; i < numLines; i = i + 1) {
x = coords[0].x;
x1 = coords[1].x;
x2 = coords[2].x;
y = coords[0].y;
y1 = coords[1].y;
y2 = coords[2].y;
float t = (float) i / (numLines - 1);
float startX = x + t * (x1 - x);
float startY = y + t * (y1 - y);
float endX = x1 + t * (x2 - x1);
float endY = y1 + t * (y2 - y1);
line(startX, startY, endX, endY);
}
}
Finally, since your drawing on a black background, and the default stroke colour is black, use strokeColour() to change the colour of the lines so that you can see the envelope once its drawn.

Discontinuous Derivative of Perlin Noise

So I've been trying to implement Perlin noise recently, and have run into some unusual problems. Whenever the edges of the grid in which the random vectors are stored are crossed, the derivative appears to be discontinuous.
Here's a link to a picture of the output (on the right), along with a 1 dimensional slice (on the left).
The Output
class perlin{
private double[][][] grid;
public perlin(int x,int y, int seed){
Random r = new Random(seed);
grid = new double[x+1][y+1][2];
for(int i=0;i<grid.length;i++){
for(int j=0;j<grid[0].length;j++){
grid[i][j][0] = 2*r.nextDouble()-1;
grid[i][j][1] = 2*r.nextDouble()-1;
}
}
}
public static double lerp(double a, double b, double t){
double c = t * t * t * (t * (t * 6 - 15) + 10);
return (b * c) + (a * (1 - c));
}
public double get(double x, double y){
double x2;
double y2;
double x3;
double y3;
x2 = x * (grid.length-1);
y2 = y * (grid[0].length-1);
x3 = down(x2);
y3 = down(y2);
x2 = x2 - x3;
y2 = y2 - y3;
int i = (int) (x3);
int j = (int) (y3);
return lerp(lerp(dot(x2, y2, grid[i][j][0], grid[i][j][1] ), dot(1 - x2, y2, grid[i + 1][j][0], grid[i + 1][j][1]),x2), lerp(dot(x2, 1 - y2, grid[i][j + 1][0], grid[i][j +1][1] ), dot(1 - x2,1 - y2, grid[i + 1][j + 1][0], grid[i + 1][j + 1][1] ), x2),y2 );
// return 0;
}
public static double dot(double x1, double y1, double x2, double y2){
return x1 * x2 + y1 * y2;
}
private static double down(double a){
if (a == 0){
return 0;
}
if(a == Math.floor(a)){
return a - 1;
}else{
return Math.floor(a);
}
}
}
From what I understand about the math behind this, the derivative of the noise should be continuous at all points, but that does not appear to be the case.

Making a circle in an array (Tile based game light map)

I am making a tiled based game in java and I want to make a light map.
I am having some issues. I have the lightmap array that has lights placed on it that affect the array. Lights emit in a circle shape. It seems ok so far but its not exactly what I wanted.
Here is my code so far:
for(float i = 0; i < strength + 1; i++){
for(double u = 0.0f; u < 360; u += 0.5){
double angle = u * Math.PI / 180;
int x2 = (int)(x + i * Math.cos(angle));
int y2 = (int)(y + i * Math.sin(angle));
if(map[y2][x2] > 1 - 1 / i)
map[y2][x2] = 1 - 1 / i;
}
}
Result:
As you can see in the result, it seems as though the light is expanding too much on the bottom left side (red x's). How do I fix this?
Background info:
Strength:
The radius of how far the light reaches. This also
determines how bright the light will be at each tile of the array.
The Array "map" is a 2D float array. The engine I am using uses float
values for the alpha channel. The range is 0 (completely transparent)
to 1 (completely opaque).
Solution (Thanks to Gene):
for(int x2 = -strength; x2 <= strength; x2++){
for (int y2 = -strength; y2 <= strength; y2++) {
double r = Math.sqrt(x2 * x2 + y2 * y2);
double inv_rad = r <= strength + 1 ? 1 / r : 0;
if(map[y + y2][x + x2] > 1 - (float) inv_rad)
map[y + y2][x + x2] = 1 - (float) inv_rad;
}
}
Your algorithm suffers from integer truncation of the map indicies. Try it the other away around. Compute the distance from each pixel in a square surrounding the center to the center. From this distance calculate what the intensity ought to be. It will be something like this:
for (x = -R; x <= R; x++)
for (y = -R; y <= R; y++) {
double r = Math.sqrt(x * x + y * y);
double inv_rad = r <= R ? 1 / r : 0; // truncate outside radius R
map[yc + y][xc + x] = 1 - inv_rad;
}
Here xc and yc are the integer center coordinates. R is the half-size of the box around the center.
when i try to add this to my project i only get o.o back
the values i entered where 500, 500,50
private float map[][] = new float[1000][1000];
public void test(int x, int y, float strength){
public void addLight(int x,int y,int strength ){
for(int x2 = -strength; x2 <= strength; x2++){
for (int y2 = -strength; y2 <= strength; y2++) {
double r = Math.sqrt(x2 * x2 + y2 * y2);
double inv_rad = r <= strength + 1 ? 1 / r : 0;
if(map[y + y2][x + x2] > 1 - (float) inv_rad)
map[y + y2][x + x2] = 1 - (float) inv_rad;
System.out.println(map[y + y2][x + x2]);
}
}
}

Need help on flippingHorizontal assignment

I am trying to flip a square image inside a picture with user input variables. After running the code nothing happens at all. In other attempts I was able to make a small thin rectangle appear. How do I fix this?
public void flipHorizontal (int x, int y, int size)
{
int half = size / 2;
int x1 = x - half;
int y1 = y - half;
int x2 = x + half;
int y2 = y + half;
Pixel sourcePixel = getPixel (x1,y1);
Pixel targetPixel = getPixel (x1, y1 + half);
//loop through columns
for (x = x; x < x2; x++)
{
//loop from 0 to before mirror point
for (y = y; y < y2 + half; y++)
{
Color friend = sourcePixel.getColor();
sourcePixel = getPixel (x1,y1);
targetPixel = getPixel (x1, y1 + half);
targetPixel.setColor (friend);
}
}
}

Categories

Resources