I found a practice java class and I am trying to get all points on the line segment such that for every point x and y are both integers but I am having trouble of implementing it. I am just looking for a formula I can use to solve the problem. or explain me how it get point (3,2). Thanks
/**
*
* #return an array containing all points on the line segment such that for every point in the array,
* both x and y are integers
* For example, if there is a line from (1,1) to (5,3), the only other point for which both x and y are
* integers is (3,2). Thus the method should return the array containing (1,1), (3,2) and (5,3)
* The order of points in the array returned should be from the point with lower x
* (use Point a as the starting point in case a.x == b.x).
*
* Hence if a = (5,1) and b=(1,3), the method returns an array such that
* first item is (1,3), second is (3,2) and third is (5,1)
*/
public Point[] getPointsOnCrosshair() {
Point[] points = new Point[20];
int counter = 0;
//getting slope of the line
double rise = this.b.y - this.a.y;
double run = this.b.x - this.a.x;
double m = rise/run;
System.out.println(m);
//getting value of c -> y = mx + c
double c = a.y - (m * a.x);
int start = 0;
int end = 0;
if (b.x >= a.x){
start = a.x;
end = b.x;
}
else{
start = b.x;
end = a.x;
}
// slope intercept y - y1 = m (x-x1)
// y = (m (x-x1)) + y1
double y = 0;
for (int a = start; a <= end; a++){
y = (m * a) + c;
if (y == (int) y){
points[counter] = new Point(a, (int) y);
counter++;
}
}
return points;
}
First, determine if the line is mostly horizontal or mostly vertical.
▉ ▉ ▉
▉ ▉ ▉
▉ ▉ ▉
▉
▉
▉
If it is mostly horizontal, you iterate x from a.x to b.x and calculate y.
If it is mostly vertical, you iterate y from a.y to b.y and calculate x.
I'll leave it to you to build the right formula, or to go find it on the web, i.e. do some research.
Brute force solution is really simple:
int p1x = 1;
int p1y = 1;
int p2x = 5;
int p2y = 3;
int xdiff = p2x - p1x;
int ydiff = p2y - p1y;
for (int x = p1x + 1; x < p2x; ++x) {
float y = ((float) x - p1x) / xdiff * ydiff + p1y;
if (((int) y) == y) { // check if xx.0
System.out.print(x);
System.out.print(", ");
System.out.println((int)y);
}
}
But I'm sure there's a more elegant way, maybe using prime factorization
Related
I want to create a "spiral effect" with particles (or any entities) in Java.
I'm new to objective programming (and also Java), so I started with something easier. I firstly created a Path object that has a value of Locations[] signed to it, it gets from the user a: Start location, End location, and double value, that tells him, how much space between each location in the path he has.
private void setLocations() {
//initialize vars
Location start = getStart();
World world = start.getWorld();
Location[] locations = new Location[amount];
double x = start.getX();
double y = start.getY();
double z = start.getZ();
//loop that will set values for locations
for (int i = 0; i < amount; i++) {
locations[i] = new Location(
world,
x + dividedDistanceX * (i + 1),
y + dividedDistanceY * (i + 1),
z + dividedDistanceZ * (i + 1)
);
}
this.locations = locations;
}
Now you might be asking what is the amount? So simply it's the number of points that are created when the object is initialized. It's simple math like getting the longest distance from point to point, and then dividing it by the value of space between each point.
Now the situation gets a little more complicated, so I prepared graphics for you:)
I want to rotate points around the longest axis to form some form of a spiral, and I want from user to set the maximum distance between the starting point and the new one.
Something like this:
And another graph of the sinusoid around one vector (x, y)
Honestly, I need some help.
Here's GitHub object link
Things I know I need to do:
Get the axis around which I will rotate point (it's the longest distance between points)
Add some value to the rest values (x+something, y+something)
Add angle, that point will rotate with, (for example each point will be rotated by 22,5).
Okay, so i did it, it wasn't even that hard:
public Location[] createSpiral(double radius, float angle, Location[] path) {
final int length = path.length;
Location[] result = path.clone();
Location start = path[0];
Location end = path[length - 1];
double startX = start.getX();
double startY = start.getY();
double startZ = start.getZ();
double endX = end.getX();
double endY = end.getY();
double endZ = end.getZ();
double distanceX = setDistance(startX, endX);
double distanceY = setDistance(startY, endY);
double distanceZ = setDistance(startZ, endZ);
double highestOffset = getHighestOffset(new double[]{distanceX, distanceY, distanceZ});
if (highestOffset == abs(distanceX)) {
for (int i = 0; i < length; i++) {
double sin = radius * sin(angle * i / length);
double cos = radius * cos(angle * i / length);
result[i].setY(result[i].getY() + cos);
result[i].setZ(result[i].getZ() + sin);
}
} else if (highestOffset == abs(distanceY)) {
for (int i = 0; i < length; i++) {
double sin = radius * sin(angle * i / length);
double cos = radius * cos(angle * i / length);
result[i].setX(result[i].getX() + cos);
result[i].setZ(result[i].getZ() + sin);
}
} else if (highestOffset == abs(distanceZ)) {
for (int i = 0; i < length; i++) {
double sin = radius * sin(angle * i / length);
double cos = radius * cos(angle * i / length);
result[i].setX(result[i].getX() + cos);
result[i].setY(result[i].getY() + sin);
}
} else {
return path;
}
return result;
}
It's just
double sin = radius * sin(angle * i / length);
double cos = radius * cos(angle * i / length);
and adding those values to corresponding X, Y if Z has the highest distance from a location, etc.
The rest of the code and methods are located in the GitHub link above.
I am working through the solution to the following problem:
Given two squares on a two-dimensional plane, find a line that would
cut these two squares in half. Assume that the top and the bottom
sides of the square run parallel to the x-axis.
This is the solution in the book:
public class Square {
public double left;
public double top;
public double bottom;
public double right;
public double size;
public Square(double left, double top, double size) {
this.left = left;
this.top = top;
this.bottom = top + size;
this.right = left + size;
this.size = size;
}
public Point middle() {
return new Point((this.left + this.right)/2.0, (this.top + this.bottom)/2.0);
}
public boolean contains(Square other) {
if (this.left <= other.left && this.right >= other.right && this.top <= other.top && this.bottom >= other.bottom) {
return true;
}
return false;
}
/* Return the point where the line segment connecting mid1 and
* mid2 intercepts the edge of square 1. That is, draw a line
* from mid2 to mid1, and continue it out until the edge of
* the square. */
public Point extend(Point mid1, Point mid2, double size) {
/* Find what direction the line mid2 -> mid1 goes */
double xdir = mid1.x < mid2.x ? -1 : 1;
double ydir = mid1.y < mid2.y ? -1 : 1;
/* If mid1 and mid2 have the same x value, then the slope
* calculation will throw a divide by 0 exception. So, we
* compute this specially. */
if (mid1.x == mid2.x) {
return new Point(mid1.x, mid1.y + ydir * size / 2.0);
}
double slope = (mid1.y - mid2.y) / (mid1.x - mid2.x);
double x1 = 0;
double y1 = 0;
/* Calculate slope using the equation (y1 - y2) / (x1 - x2).
* Note: if the slope is “steep” (>1) then the end of the
* line segment will hit size / 2 units away from the middle
* on the y axis. If the slope is “shallow” (<1) the end of
* the line segment will hit size / 2 units away from the
* middle on the x axis. */
if (Math.abs(slope) == 1) {
x1 = mid1.x + xdir * size / 2.0;
y1 = mid1.y + ydir * size / 2.0;
} else if (Math.abs(slope) < 1) {
x1 = mid1.x + xdir * size / 2.0;
y1 = slope * (x1 - mid1.x) + mid1.y;
} else {
y1 = mid1.y + ydir * size / 2.0;
x1 = (y1 - mid1.y) / slope + mid1.x;
}
return new Point(x1, y1);
}
public Line cut(Square other) {
/* Calculate where a line between each middle would collide with the edges of the squares */
Point p1 = extend(this.middle(), other.middle(), this.size);
Point p2 = extend(this.middle(), other.middle(), -1 * this.size);
Point p3 = extend(other.middle(), this.middle(), other.size);
Point p4 = extend(other.middle(), this.middle(), -1 * other.size);
/* Of above points, find start and end of lines. Start is farthest left (with top most as a tie breaker)
* and end is farthest right (with bottom most as a tie breaker */
Point start = p1;
Point end = p1;
Point[] points = {p2, p3, p4};
for (int i = 0; i < points.length; i++) {
if (points[i].x < start.x || (points[i].x == start.x && points[i].y < start.y)) {
start = points[i];
} else if (points[i].x > end.x || (points[i].x == end.x && points[i].y > end.y)) {
end = points[i];
}
}
return new Line(start, end);
}
public String toString() {
return "(" + left + ", " + top + ")|(" + right + "," + bottom + ")";
}
}
I have the following questions:
What is the size parameter in the extend function? The size of what? A side? The entire square (how can we quantify the size of the entire square?) Area?
In the cut() function, what are p1 through p4 ultimately doing? And why do we need 4 points to do it?
As I understand the problem, it's necessary and sufficient for the line to connect the two centres.
Assume that the top and the bottom sides of the square run parallel to the x-axis.
I'm not aware of why this assumption is necessary.
This answer might not be relevant to the TS. But in case if you are also wondering about this answer, the parameter "size" is misleading - it is referring to the length of the square (Eg. 3x3 square has the length of 3.) Why four points? Because the question assumes that the line here represents the two "farest" points that cut across two squares. Each square has two points that has the potential to become start/end of the line.
I have a diamond drawn on a panel and I'm trying to calculate if the mouse position is within its bounds. The problem is trying to the diamond, it needs four coordinates making up four lines to create the shape.
The easy thing to do is to create/fit a square within the bounds of the diamond, but I want to account for the remaining triangular areas outside. I initially thought I trying to calculate the slope between two points and figuring out if the x and y of the mouse intersects the line by adding the slope into the the equation, but it proved to be a lot more difficult when it comes to the lines that make up the TOP->RIGHT, RIGHT->BOTTOM and BOTTOM->LEFT given that the center of the diamond is not zero.
Is there an easier implementation to check if the mouse's x,y is within the bounds of the diamond?
In pseudo-code (to be more readable):
Point org = new Point(64, 32); // Center.
Point radii = new Point(32, 16); // Half the size.
Point mousePos = ...
mousePos -= org; // Relative to the center.
boolean inside = Math.abs(mousePos.x) * radii.y + Math.abs(mousePos.y) * radii.x
<= radii.x * radii.y;
Of math one knows (0, radii.y) and (radii.x, 0) determine the border line in the positive quadrant.
That comes down to the formula above.
static boolean isInsideDiamond(int x, int y, int[] xs, int[] ys) {
int minX = xs[0];
int maxX = minX;
int minY = ys[0];
int maxY = minY;
for (int i = 1; i < 4; ++i) {
minX = Math.min(minX, xs[i]);
maxX = Math.max(maxX, xs[i]);
minY = Math.min(minY, ys[i]);
maxY = Math.max(maxY, ys[i]);
}
int orgX = (minX + maxX) / 2;
int orgY = (minY + maxY) / 2;
int radX = (maxX - minX) / 2;
int radY = (maxY - minY) / 2;
return isInsideDiamond(x, y, orgX, orgY, radX, radY);
}
static boolean isInsideDiamond(int x, int y, int orgX, int orgY, int radX,
int radY) {
x -= orgX;
x = Math.abs(x);
y -= orgY;
y = Math.abs(y);
return x * radY + y * radX <= radX * radY;
}
By the way:
Polygon diamond = new Polygon(xs, ys, 4);
boolean inside = diamond.contains(x, y);
I have been searching the internet for about a day and I can not seem to find an algorithm to find all points within a bounding box.
Picture:
The accuracy also needs to be 8 decimal digits long.
Example:
(0,0)
(0,0.00000001)
(0,0.00000002)
etc..
(80,80.45356433)
I don't know what exactly you mean by finding all points inside a box. There is just too many!
I would suggest using an Identifier function, i.e., given a point (x, y), I(x, y) = 1 if and only if the point (x, y) is inside your box.
A call to the identifier in your example requires $4$ comparisons.
public class Box {
/**
* Identifier function.
* #param x x-coordinate of input point
* #param y y-coordinate of input point
* #return True if and only if the given point is inside the box.
*/
public boolean IsInside(double x, double y) {
return (this.x_min <= x) && (this.x_max >= x) &&
(this.y_max >= y) && (this.y_min <= y);
}
double x_min, x_max, y_min, y_max; // or do it with left top width and height
}
I hope it helps.
long p = (long)Math.pow(10, 8);
long start = 90 * p;
long end = 180 * p;
for (long i = start; i < end; i++) {
for (long j = start; j < end; j++) {
System.out.println("(" + (double)i / p + "," + (double)j / p + ")");
}
}
will take a while
I posted something similar yesterday, but got nothing. I spent a few hours today problem-solving, but didn't progress any.
I'm using Processing (the language) and trying to implement a method that draws a line between two points. (I don't want to use the library's line() method.)
My lineCreate method works great for positive slopes, but fails with negative slopes. Can you help figure out why?
Here's the lineCreate() code:
void createLine(int x0, int y0, int x1, int y1){
//...
// Handle slanted lines...
double tempDX = x1 - x0;
double tempDY = y1 - y0; // Had to create dx and dy as doubles because typecasting dy/dx to a double data type wasn't working.
double m = (-tempDY / tempDX); // m = line slope. (Note - The dy value is negative
int deltaN = (2 * -dx); // deltaX is the amount to increment d after choosing the next pixel on the line.
int deltaNE = (2 * (-dy - dx)); // ...where X is the direction moved for that next pixel.
int deltaE = (2 * -dy); // deltaX variables are used below to plot line.
int deltaSE = (2 * (dy + dx));
int deltaS = (2 * dx);
int x = x0;
int y = y0;
int d = 0; // d = Amount d-value changes from pixel to pixel. Depends on slope.
int region = 0; // region = Variable to store slope region. Different regions require different formulas.
if(m > 1){ // if-statement: Initializes d, depending on the slope of the line.
d = -dy - (2 * dx); // If slope is 1-Infiniti. -> Use NE/N initialization for d.
region = 1;
}
else if(m == 1)
region = 2;
else if(m > 0 && m < 1){
d = (2 * -dy) - dx; // If slope is 0-1 -> Use NE/E initialization for d.
region = 3;
}
else if(m < 0 && m > -1){
d = (2 * dy) + dx; // If slope is 0-(-1) -> Use E/SE initliazation for d.
region = 4;
}
else if(m == -1)
region = 5;
else if(m < -1){
d = dy + (2 * dx); // If slope is (-1)-(-Infiniti) -> Use SE/S initialization for d.
region = 6;
}
while(x < x1){ // Until points are connected...
if(region == 1){ // If in region one...
if(d <= 0){ // and d<=0...
d += deltaNE; // Add deltaNE to d, and increment x and y.
x = x + 1;
y = y - 1;
}
else{
d += deltaN; // If d > 0 -> Add deltaN, and increment y.
y = y - 1;
}
}
else if(region == 2){
x = x + 1;
y = y - 1;
}
else if(region == 3){ // If region two...
if(d <= 0){
d += deltaE;
x = x + 1;
}
else{
d += deltaNE;
x = x + 1;
y = y - 1;
}
}
else if(region == 4){ // If region three...
if(d <= 0){
d += deltaSE;
x = x + 1;
y = y + 1;
}
else{
d += deltaE;
x = x + 1;
}
}
else if(region == 5){
x = x + 1;
y = y + 1;
}
else if(region == 6){ // If region four...
if(d <= 0){
d += deltaSE;
x = x + 1;
y = y + 1;
}
else{
d += deltaS;
y = y + 1;
}
}
point(x, y); // Paints new pixel on line going towards (x1,y1).
}
return;
}
Have a look at this page. It explains the whole theory behind line drawing with code examples.
There are a number of known algorithm for line drawing. Read about them here.