I'm making my own Minecraft world border plugin for myself use.
I have a spawnpoint, that can be any coordinates, and I check the distance between player and the spawnpoint. If the distance exceeds 1000 (that is the radio) I cancel the user movement.
So I can say my circle radio is 1000.
The problem is: I want to draw in the world the circle borders, so the player can see the world borders. I need to iterate over the coordinates of the circumference, and I dont have any idea of how to do that (my maths are really poor)
Thanks you
Usually to iterate through the coordinates of a circe you want to use a bit of trigonometry.
You create a variable angle which starts from 0 and you iterate it by the step you need until 2π, then your x coordinate will bee cos(angle) and your y coordinate will be sin(angle).
Then probably you will have to round a bit those value considering you want to use them in a minecraft word where you can't place blocks in decimal coordinates
Related
Lets say I have a circle, and if the user drags his mouse clockwise along the path of the circle the counter increases, and if he drags the mouse counter-clockwise it decreases. What is the best way to implement this in Java? I imagine trig will be needed so the program knows when the user is dragging clockwise or counter-clockwise, right? Not looking for code examples, just help with theory so I can begin with the right approach.
As you probably have access to the circle, get its center point coordinates.
Then get the coordinates of the mouse. After that you compute the angle between the vector and the x-axis.
You do so by first setting the circle point as the center of the imaginary coordinate system and then shifting the mouse coordinates to this system. After that you apply atan2 on the new mouse coordinates. The result is the desired angle.
Point center = ...;
Point mouse = ...;
Point shiftedMouse = new Point(mouse.x - center.x, mouse.y - center.y);
double angle = Math.atan2(shiftedMouse.y, shiftedMouse.x);
At this point you probably need to convert the result of angle to degrees or something like that, if you like. You may take a look at Wikipedia#atan2 for this.
Of course you can also leave it in the format (-pi, pi] and work with that, if you know what it means.
Now you track how this angle changes. If it increases, then the mouse is moving counter-clockwise; if it decreases, then clockwise and so on (or maybe the other way around, just try it). Take care of the bound where after 359° 0° and then 1° comes.
I'm making a mini game where the player is a ball in the middle of the screen and shoots bullets guided by the mouse. I thought in order to guide the projectiles I could work out the gradient between the player and the mouse click, and increase the x and y values of a small rectangle by the gradient incrementally, at every paint. The problem with this however, is that 9 times out of 10, the gradient is a fraction, and is therefore impossible to use since x and y requires an int. Are there any possible workarounds or is this task simply impossible..?
Let's say I have many small bitmaps and I draw a big circle around them but not necessary all bitmaps are inside of the circle (like some can be half way in or have their edges stick out) and I want to run on every single pixel of the bitmaps in the circle (meaning pixels that are outside of the circle wont be counted, only the parts that are inside), how do I go about doing that, I know how to run on every pixel of all the bitmap, but not in a specific shape..
You need to create an imaginary grid, or rather a grid that is only useful in that it will help you solve the problem at hand. This is the grid that you will assign all the bitmaps to a position on, imagining that the circle's center is to be located at (0,0).
You then use a little math
to find if a pixel as it is relative to its bitmap's position on the grid, is within the radius of the circle.
Of course the distance formuala is
Or if you rather it is the sqrt( a^2 + b^2 ). where 'a' is the difference in x and 'b' is the difference in y between 2 points.
I'm making a coastline fractal on a window that is one by one wide, and I would like to make the very first one pictured below, however, I cannot figure out which x and y coordinates to use to make the angles form 90 degrees and still fit on the screen, I don't need any code, I just would like how to figure out which x and y coordinates to use. Thanks!
Points:
1st point: (0,0.5)
2nd point: (0.25,0.75)
3rd point: (0.75,0)
4th point: (1,0.5)
My work (although messy and illegible at times):
It looks like from the picture that the first and last point both have a y-value of 0.5. Since the viewing window is one, you divide it into 4 parts each of which is 0.25 in length. The triangles that are formed if you draw a horizontal line at y=0.5 are isosceles according to the image. Thus, you solve: sin(45)=x/0.5.
re "x and y coordinates are doubles in between 0 and 1",
Then you will need to translate from your model (the set of points that make up your fractal) and the view (the GUI display). The model will go from 0 to 1, the view from 0 to the graphical window's width. A simple linear transformation where you multiply the model by some scale factor will serve.
Seems like you're wanting to map an abstract coordinate system to your screen.
Let's say your endpoints (in arbitrary coordinates) are (0, 0) and (1, 0). Then your points for the leftmost figure, in this system, will be (0, 0), (1/4, sqrt(2)/4), (1/2, 0), (3/4, -sqrt(2)/4), and (1, 0).
The other diagrams are calculated by some method. It sounded like your question was focusing on fitting it to the screen, so I'll continue with that. The method for fitting it to the screen is the same.
From there, you have a screen coordinate system. Each point is transformed to the screen. Let's say you have a 1000 by 1000 screen, with screen coordinates (0, 0) in the upper left. If you want to take up the entire screen, then you'd do the following:
Flip the y coordinates (+y is down on your screen)
Determine the full range in x and y for your arbitrary coordinates (1 for x, sqrt(2)/2 for y)
Multiply x values by 1000, and y values by 2000 / sqrt(2) to expand to the screen.
Subtract 500 from y values to center the image in the y direction.
I have an array: int[][] lawn = new int[980][1280];
wich stores the values of the height of blades in the virtual “lawn”.
In my simulation i have a robot that goes around the lawn and cuts the blades.
My robot has the form of a circle with a diameter (rDiameter). The coordinate system is done with Double and my lawn is in Integer.
I have to develop an algorithm that puts to 0 all the cells touched by the robot when it moves around.
I have the start and end point of the movement, which are stored in a Line2D.Double form and I want to set on 0 all the cells touched by robot (image).
Any ideas?
(Here my previous question on the same argument every cartesian point in a circle
Break the problem in 3 parts. Part 1 is to set to 0 all points in a semi-circle. Part 2 is to set to 0 all points in a rectangle. Part 3 is to break the path into two semi-circles (at the ends) and a rectangle (joining them).
Note that the semi-circles and the rectangles have, in general, lines that are not axis aligned. There are plenty of references out there on rasterizing polygons and circles. You could look up Jack Bresenham's algorithms. Or you could flip open any classic computer graphics text.