Saturday, April 30, 2016

How to break out of a nested for-loop in Java

You'd normally think that it's impossible to break out of a double, triple or n-times nest loops in Java without using some convoluted piece of code that  involves some kind of boolean checking  but it is possible, and it's pretty darn simple. It's just one of those things that you never learned about in your high school or college java class.

Anyway, here it is, with all its beauty:

outerloop: for(int i = 0; i < 20; i++) {
       for(int j = 0; j < 10; j++) {
            if(i == 10) {
               break outerloop;
            }
       }
}

Yes, it's that simple. Who knew you could label loops in Java.

Saturday, March 2, 2013

Efficient Vectors in C#

Making the raytracing made me in an efficiency craze. I had to think about the efficiency for everything. One of the basic things to think about is vectors! Consider the following vectors. In this code, there are three vector structs, each with different x, y, z implementations. The three vectors overload the multiplication, addition, and subtraction operators to apply them to x, y, and z. I created a simple test to see the efficiency of the 3 implementations. The test was very simple it involved two vectors one with the x, y, and z equal to PI and vector with the x, y, and z values set to 1. The vector with the 1 value is multiplied, added, and subtracted with a PI vector in a for loop that goes 1,000,000 times. Here are the results:

Struct * + -
Vec
1665
255
316
Vec2
2104
569
781
Vec3
1603
224
346
The result show that properties are the least efficient way, taking 100% more time.

Monday, December 24, 2012

Online Raytracer

I have finished 95% percent of the raytracer, and since it is written in C# and doesn't use unsafe code, I could use it with asp.net. A parser interprets XML for the raytracer and produces a final image at the bottom of the page. The online sample lacks textures and meshes, which I plan to add later. Also the documentation of the markup language is not complete, but you can get the hang of it by taking a look at the samples. Everyday I will update the raytracer and parser for the bugs. Please use the report page to report any bug or exception that comes up! You can find the website here.

Monday, September 17, 2012

Update: More raytracer images

I haven't posted in a while. Today I will not post much except some more of images of my C# raytracer. I have added reflection, transparency, and textures.
Here are the images:

Friday, September 7, 2012

Break: Calculating perfect numbers

Till now there have been 4 perfect numbers discovered, but it would be also nice to know how to calculate them. The logic is very simple. I will write the logic and code in C++/CLI.
First lets define perfect numbers. Perfect numbers are numbers that are equal to their divisors. For example number 6, its divisors are 1,2,3 (excluding the number itself). 1 + 2 + 3 = 6. There are about 47 perfect numbers discovered till now, the first 4 are very easy to get; the rest go from 8 digits to millions of digits. The logic is very simple, Here is the code:

long count = 1;
for( ; ;)
{
long current = 0;
for(long i = count - 1; i  > 0; i--)
{
if(count % i == 0) // make sure the current number is divisble with the numbers smaller than it
{
 current += i;
}
}
if(current == count)
{
Console::WriteLine(count);
}
count++;
}

Monday, August 27, 2012

C# raytracer from scratch

I am building a complete C# raytracer. It uses my own math library and it will be part of the Disque Engine. The raytracer is close to completion. Right now it can render the basic shapes such as spheres, cubes, cones, cylinders, disks, rectangles, and triangles. I am also trying to implement a ply file reader so that I can render the Stanford bunny or other standard models. After I finish the raytracer I will post tutorials on how to build your own.

Here are some samples(time format is h:m:s.ms) :

Simple image rendered at 256 samples per pixel, total render time: 00:09:43.838

An image of 1000 random spheres rendered at 121 samples per pixel, total render time: 02:13:45.002



I will provide more samples as the raytracer advances.

Thursday, July 12, 2012

How to create a sphere programmatically in XNA


I "discovered" an easy but silly way to create a sphere programmatically in XNA, it is very simple. The idea came to me when I wanted to create huge planets in XNA to make an endless map. Any way the method is very simple, I don't know If someone knows it or it was used before, but till now it is working and efficient. You can control the resolution or how "round" the sphere is.
The code is written in C# with microsoft's XNA 4.0.
Here is a high resolution sphere in wire frame:
The sphere contains 8100 vertices and 48600 indices.

Code:

    public class Sphere
    {
        VertexPositionColor[] vertices; //later, I will provide another example with VertexPositionNormalTexture
        VertexBuffer vbuffer;
        short[] indices; //my laptop can only afford Reach, no HiDef :(
        IndexBuffer ibuffer;