Java: getting points of a circle with boolean filled - java

I need a method to get the points of the circle, I have one that I found online but unfortunately I'd like to add a boolean filled to it:
public static Location[] getCylinderAt(Location loc, int r, int height) {
ArrayList<Location> list = new ArrayList<>();
int cx = loc.getBlockX();
int cy = loc.getBlockY();
int cz = loc.getBlockZ();
World w = loc.getWorld();
int rSquared = r * r;
for (int x = cx - r; x <= cx + r; x++) {
for (int y = cy - height; y <= cy + height; y++) {
for (int z = cz - r; z <= cz + r; z++) {
if ((cx - x) * (cx - x) + (cz - z) * (cz - z) <= rSquared) {
list.add(new Location(w, x, y, z));
}
}
}
}
return list.toArray(new Location[list.size()]);
}
I can't really get my head around the maths involved in this and have been searching through non minecraft sources to create my own but to no avail.
Ideally I'd like to be able to change the method to this:
public static Location[] getCylinderAt(Location loc, boolean filled, int r, int height)
Thanks guys! If you like I can remove all of the minecraft references, but I didn't think it'd be necessary as a Location is basically a Vector with a few added minecraft only variables!
Thanks for reading :)

Are you looking for a way to compute pixels on the rim of a circle, as opposed to those inside, for the case where filled is false? If so, have a look at the midpoint circle algorithm. It describes how a circle can be drawn in a raster image.

Related

Trying to efficiently hollow out a sphere in minecraft (fabric modding, 1.16.5)

I am trying to hollow out a sphere, I already achieved this however it is incredibly slow (A few seconds for a sphere with a radius of 5, a few minutes for a sphere with a radius of 100)
This is my code:
BlockPos pos = player.getBlockPos();
int startX = pos.getX();
int startY = pos.getY();
int startZ = pos.getZ();
int squaredRadius = radius * radius;
int x1;
int y1;
int z1;
int flags = 2 | 8 | 16 | 32;
BlockPos.Mutable blockPos = new BlockPos.Mutable(0, 0, 0);
BlockState state = Blocks.AIR.getDefaultState();
for (int x = startX - radius; x < startX + radius; x++) {
for (int y = Math.max(0, startY - radius), maxY2 = Math.min(255, startY + radius); y < maxY2; y++) {
for (int z = startZ - radius; z < startZ + radius; z++) {
x1 = x - startX;
y1 = y - startY;
z1 = z - startZ;
if (x1 * x1 + y1 * y1 + z1 * z1 <= squaredRadius)
world.setBlockState(blockPos.set(x, y, z), state, flags);
}
}
}
How could I speed this up? (Or is it already as fast as it can be?)
EDIT: For a 100 radius it takes roughly 8 minutes on my machine
Update: I managed to fix it! What I did was make a mixin into World#setBlockState(BlockPos, BlockState, int) and check if the flags were some number I set (999 in my case), and if it where I canceled the method execution right after the block state was set, saving huge amounts of time when placing a lot of blocks. However, it did not send the chunks to the player so what you have to do is simply send all the chunks you changed to the player, I did this by storing all the chunks I changed and for each player in the world I sent a ChunkDataS2CPacket with the new chunk to the player, which fixed my problem entirely, it works like a charm :D
EDIT: It also does not do block updates/lighting updates

Color data is no being stored properly

I am attempting to make a rendering system with a depth map involved with the usual pixels for dealing with alpha. My problem is that no color is being set correctly! I have tried to debug using System.out.println and testing various components, but to no avail I have not found a solution.
The Variables
The variables that are involved with dealing with drawing, setting, and clearing of pixels are: private int[][] node, private int[] pixels, and private ArrayList<Integer> changedPixels.
private int[][] node deals with storing pixels and dealing with depth [depth][x + y * width] before transferring over to the BufferedImage pixels. The data is set to a clear black and the lowest depth it is a fully visible black.
private int[] pixels is the data from a BufferedImage to change it up, it is the only image every used! All data is by default fully visible black
private ArrayList<Integer> changedPixels deals with pixels that are there from the last frame so as to help boost FPS by not clearing the entire screen if not needed. Empty by default since not pixels were changed from a previous frame.
The Methods
I have several methods for the rendering system: setNode(int x, int y, int z, int color, int alpha, drawScreen(), and clearScreen(). I also have a drawing rectangle and sprite function which deals with adding pixels by calling the setNode() method to add in colors.
private void setNode(int x, int y, int z, int color, float alpha)
{
color = Pixel.getColor(alpha, color);
if (translate) // Move the pixel to the correct location
{
x -= transX;
y -= transY;
}
if (x < 0 || x >= width || y < 0 || y >= height || alpha <= 0.0f || nodeMap[z][x + y * width] == color) // Check if we need to draw the pixel
return;
for (int zz = z + 1; zz < maxDepth; zz++)
if (Pixel.getAlpha(nodeMap[zz][x + y * width]) >= 1)
return;
if (alpha < 1.0f) // If pixel isn't completely opaque, then set it's alpha to the given one
if (nodeMap[z][x + y * width] != color) // If color isn't equal to the one we supply, change it up correctly
color = Pixel.getColorBlend(color, nodeMap[z][x + y * width]);
if (color == Pixel.WHITE) System.out.println("Pixel is white at x: " + x + ", y: " + y);
nodeMap[z][x + y * width] = color;
}
public void drawScreen()
{
int color = clearColor;
for (int x = 0; x < width; x++)
for (int y = 0; y < height; y++)
{
for (int z = maxDepth - 1; z > 0; z--)
{
if (Pixel.getAlpha(nodeMap[z][x + y * width]) > 0f)
color = Pixel.getColorBlend(color, nodeMap[z][x + y * width]);
if (Pixel.getAlpha(color) >= 1f)
break;
}
if (pixels[x + y * width] != color)
{
pixels[x + y * width] = color;
changedPixels.add(x + y * width);
}
}
}
public void clearScreen()
{
for (Integer pixel : changedPixels)
{
for (int z = 0; z < maxDepth; z++)
{
if (z > 0)
nodeMap[z][pixel] = clearColor;
else
nodeMap[z][pixel] = bgColor;
}
}
changedPixels.clear();
}
public void drawRect(int offX, int offY, int z, int width, int height, int color)
{
for (int x = 0; x < width; x++)
for (int y = 0; y < height; y++)
setNode(x + offX, y + offY, z, color);
}
public static int getColorBlend(int color1, int color2)
{
float a1 = getAlpha(color1);
float a2 = getAlpha(color2);
float a = Math.max(a1, a2);
float r = ((getRed(color1) * a1) + (getRed(color2) * a2 * (1 - a1))) / a;
float g = ((getGreen(color1) * a1) + (getGreen(color2) * a2 * (1 - a1))) / a;
float b = ((getBlue(color1) * a1) + (getBlue(color2) * a2 * (1 - a1))) / a;
return Pixel.getColor(a, r, g, b);
}
The Test
What I do currently is initalize the rendering system and set the nodeMap and pixel map to the previously mentioned settings. After this has been completed a game engine begins and then a method in a gui button (you might need it), but it calls drawRect(0(x), 0(y), 1(z), 100(width), 20(height), Pixel.WHITE(color)) which works as I have testing to see if it's running the method and which pixels it's drawing to.
The Problem
The overall problem is that the screen is completely white, I can't quite figure out the reason! I do know it has nothing with the alpha blending, which works fine as I have used it will a previous version of a rendering system I did.
Any help is appreciate and sorry that this is quite a long question, I just wanted to make sure you had everything you may need help me solve this. I do realize this is not be very effiecent, but I still like the system. Thanks again!

Printing circle to the screen

Why am I not getting a circle printed to the screen when I run the following block of code?
It doesn't print it accurately, seems like something I'm doing wrong when it's scanning the coordinates.
public class Question2 {
public static void main(String[] args) {
DrawMeACircle(3, 3, 3); // should print to the screen an ellipse looking circle
}
public static void DrawMeACircle(double posX, double posY, double radius) {
double xaxis = 20; // scanning the coordinates
double yaxis = 20; // " "
for (double x = 0; x < xaxis; x++) {
for (double y = 0; y < yaxis; y++) {
//using the formula for a cicrle
double a = Math.abs((posX - x) * (posX - x));
double b = Math.abs((posY - y) * (posY - y));
double c = Math.abs(a + b);
double d = Math.abs(radius * radius);
// checking if the formula stands correct at each coordinate scanned
if ( c == d) {
System.out.print('#');
}
else {
System.out.print(' ');
}
}
System.out.println();
}
}
}
I'm afraid that comparing doubles like this
if ( c == d) {
System.out.print('#');
}
is a very unreliable, they're probably missing by a little bit, and you're not printing the circle where you need to.
I'd recommend checking for a range instead
double arbitraryNumber = 2;
if ( math.abs(c - d) < arbitraryNumber)) {
System.out.print('#');
}
Or, if you want a more reliable way, I'd make a 2-d char array and treat it as a coordinate system, and then fill the 2-d array with the circle and print the array.
You can fill the array with a little bit of trigonometry. Just figure out where the dot should be every few degrees(or radians) until you've gone 360 degrees

Not sure why this short processing assignment isn't working

This is a homework assignment.
Work 19 5/16 is the assignment
http://sites.stuycs.org/home/courses/ml2x/dyrland-weaver/work
I am running this in the program processing, which does not require main methods.
Blob was given to us. We had to make BlobRunner on our own.
Any advice on why my code isn't doing what its supposed to would be appreciated.
FIRST FILE BlobRunner
int popSize = 4;
int wobble = 2;
int numSides = 4;
float rad = 100;
int radInt = (int) rad;
float a = sqrt(popSize);
int rootPop = (int) a;
Blob[][] blobs = new Blob[popSize/rootPop][rootPop];
/*=====================================
The trickiest part of setup is to make
the screen an appropriate size for the
grid of blobs. The grid should be just
big enough to contain all of the blobs.
====================================*/
void setup() {
size ((popSize/rootPop)*(2*(radInt+3)), rootPop*(2*(radInt+3)));
populate();
}
/*=====================================
The main purpose of draw is to go through
the array of blobs and display each.
====================================*/
void draw() {
int createdSoFar = 0;
for (int i = 0; i<rootPop; i++){
for (int j = 0; j<popSize/rootPop; j++){
if (createdSoFar < popSize){
blobs[j][i].display();
}
createdSoFar++;
}
}
}
/*=====================================
Populate the array of blobs.
You can use any values for radius, number of sides
and wobble factor that you'd like, but you must
use x and y coordinates that ensure the blobs
are drawn in a grid without overlaping each other.
Your code should work for any reasonable value
of population (i.e. something that would fit on a
normal monitor).
====================================*/
void populate() {
for (int i = 0; i < rootPop; i++){
float y = 1;
for (int j = 0; j < (popSize/rootPop); j++){
float x = 1;
blobs[j][i] = new Blob (x*(rad+3), y*(rad+3), numSides, radInt, wobble, wobble);
x=x+2;}
y=y+2;}
}
SECOND FILE Blob
/*=====================================
A Blob object is a regular polygon variant that
can have various features.
Instance Variables:
numSides: number of sides
rad: distance from the center of the polygon
to any vertext
x: x coordinate of the center
y: y coordinate of the center
xFactor: "wobble" foctor in the x direction
yFactor: "wobble" factor in the y direction
====================================*/
class Blob {
int numSides;
int rad;
float x;
float y;
int xFactor;
int yFactor;
Blob(float cx, float cy, int sides, int r, int xf, int yf ) {
x = cx;
y = cy;
numSides = sides;
rad = r;
xFactor = xf;
yFactor = yf;
}
void display() {
float nx;
float ny;
int rx, ry;
float sy;
strokeWeight(1);
beginShape();
for( float t = 0; t <= 1; t+=( 1.0/numSides ) ) {
/*
"wobble" effect is created by adding a random number to each
x and y coordinate. The larger the x and y factors, the higher
the possible wobble value could be
*/
rx = (int)random(xFactor);
ry = (int)random(yFactor);
nx = rad * cos( 2 * PI * t ) + x + rx;
ny = rad * sin( 2 * PI * t ) + y + ry;
vertex(nx, ny);
}
endShape();
}
}
Your code runs, thus it is doing what you asked it to do and nothing more.
I asked my cat to check it out though and she was all, "the guy is re-initializing his variables inside each pass of the loop, he'll never get a grid of blobs that way. Tell him to start by moving float y = 1; float x = 1; in populate() outside of the bounds of the two for loops and start debugging from there."
Then she rolled over on to her side and I patted her.

Let an image fly in a circle in Java

I need to let an image fly in a cirle, i'm now only stuck on one part.
For calculating the points it needs to go im using pythagoras to calculate the height (point B).
Now when using the sqrt function I the the error that I can't convert a double to an int.
Here's my code :
package vogel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;
public class vogel extends Component {
private int x;
private int r;
private int b;
BufferedImage img;
public vogel() {
try {
img = ImageIO.read(new File("F:/JAVA/workspace/School/src/vogel/vogel.png"));
} catch (IOException e) {
}
r = 6;
}
#Override
public void paint(Graphics g) {
for(int i = -r; i <= r; i++) {
x = i;
b = Math.sqrt(r^2 - x^2);
g.drawImage(img, x, b, this);
}
}
public static void main(String[] args) {
JFrame f = new JFrame("Boot");
f.setSize(1000,1000);
f.add(new vogel());
f.setVisible(true);
for (int number = 1; number <= 1500000; number++) {
f.repaint();
try {
Thread.sleep(50);
} catch (InterruptedException e) {}
}
}
}
Hope one of you guys can help me out
Cast the value. E.G.
b = (int)Math.sqrt(r^2 - x^2);
convert it by casting
b = (int)Math.sqrt(..);
although using the algorithm of Bresenham is more efficient than calculating over roots
b = (int)Math.sqrt(r^2 - x^2);
This line:
b = Math.sqrt(r^2 - x^2);
...Isn't doing what you think in a number of ways. To start with ^ means XOR, it's not an exponent operator - and it returns a double where as b is an int.
Dealing with the power problem, we can use Math.pow instead (which actually gives you a power) to get:
b = Math.sqrt(Math.pow(r, 2), Math.pow(x, 2));
Of course, I'm assuming here you did mean power and didn't mean to XOR the two numbers together instead!
You could just cast the result to an int:
b = (int)Math.sqrt(Math.pow(r, 2), Math.pow(x, 2));
But you probably want to change b so it's a double and you can keep the added accuracy.
Pre-calculating the path co-ordinates would speed up the redraw loops, the quickest way to do get every pixel co-ordinate is with the Bresenham method (ref. Hachi), here is the Java code
private void drawCircle(final int centerX, final int centerY, final int radius) {
int d = 3 - (2 * radius);
int x = 0;
int y = radius;
Color circleColor = Color.white;
do {
image.setPixel(centerX + x, centerY + y, circleColor);
image.setPixel(centerX + x, centerY - y, circleColor);
image.setPixel(centerX - x, centerY + y, circleColor);
image.setPixel(centerX - x, centerY - y, circleColor);
image.setPixel(centerX + y, centerY + x, circleColor);
image.setPixel(centerX + y, centerY - x, circleColor);
image.setPixel(centerX - y, centerY + x, circleColor);
image.setPixel(centerX - y, centerY - x, circleColor);
if (d < 0) {
d = d + (4 * x) + 6;
} else {
d = d + 4 * (x - y) + 10;
y--;
}
x++;
} while (x <= y);
}
You will need to adjust slightly for your own implementation as this example uses the data storage type defined by Rosetta.
http://rosettacode.org/wiki/Basic_bitmap_storage#Java
Note: because it generates a 1/8 arc and mirrors it, it won't create the co-ordinates in the correct order to move your image - you will need to load them into an array and sort them.
The complete class can be found at Rosetta here; http://rosettacode.org/wiki/Bitmap/Midpoint_circle_algorithm#Java
more information about Bresehnam's equations can be found here
http://free.pages.at/easyfilter/bresenham.html

Categories

Resources