Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (476)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (530)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Drawing a 1-pixel outline for an ellipse  (Read 741 times)
0 Members and 1 Guest are viewing this topic.
Offline HeroesGraveDev

JGO Kernel


Medals: 238
Projects: 11
Exp: 2 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Posted 2014-01-01 06:51:41 »

Well, not just pixels, it can also be applied to tile maps.

The math behind solving this problem is
(ex^2 / rx^2) + (ey^2 / ry^2) = 1

Where rx is x-radius, ry is y-radius, ex is the distance from an x-coordinate to the centre, and ey is the distance from a y coordinate to the centre.

The extents (e[xy]) must be lower than their respective radii.

If the x and y extents are too small, the point is inside the circle and
(ex^2 / rx^2) + (ey^2 / ry^2) < 1
.
If the x and y extents are too large, the point is outside the circle and
(ex^2 / rx^2) + (ey^2 / ry^2) > 1
.

At first you'd think to just check every pixel/tile in range with that formula, but that doesn't work, because you are taking integers into an equation that need floating-point precision. You would end up with a very irregular line, and the chances are lots of parts would be missing.

The proper way to solve this is to take floats into the equation and later floor/ceil them to integers. The way to do this is take the coordinate on one axis and use it to solve the other.

To solve this equation for a point on the x-axis, we need to find ey:

1  
2  
3  
4  
(ex^2 / rx^2) + (ey^2 / ry^2) = 1   // Starting equation
1 - ex^2 / rx^2 = ey^2 / ry^2       // Subtract (ex^2 / rx^2) from both sides
(1 - ex^2 / rx^2) * ry^2 = ey^2     // Multiply both sides by ry^2
sqrt((1 - ex^2 / rx^2) * ry^2) = ey // And get the square root


and then do the same but using a point on the y-axis to get ex.

Once you have those points, you add/subtract them from the centre and turn them into ints.
Make sure that you do opposite operations with floor/ceil to make sure the ellipse is symmetrical.

Code

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
// Get the y-coord for each x-coord
for(int i = (int) (cx - rx); i <= cx + rx; i++)
{
   float ex = (float) i - cx; // ex = extent from the centre to a coordinate 'i'
 
   float j = 1f - ((ex * ex) / (rx * rx));
   j = j * ry * ry;
   j = (float) Math.sqrt(j);
   
   // Then you can just add the y-extent to the centre and you get the coordinates!
  doSomething(i, MathUtils.floor(cy + j));
   doSomething(i, MathUtils.ceil(cy - j));
}

// Get the x-coord for each y-coord.
// This is the same as the first part, but in order yx instead of xy, to fill in the gaps.
for(int j = (int) (cy - ry); j <= cy + ry; j++)
{
   float ey = (float) j - cy;
   
   float i = 1f - ((ey * ey) / (ry * ry));
   i = i * rx * rx;
   i = (float) Math.sqrt(i);
   
   doSomething(MathUtils.floor(cx + i), j);
   doSomething(MathUtils.ceil(cx - i), j);
}


Hopefully someone finds this useful. If you find any bugs/typos, please report them so other people get working code.

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

pw (12 views)
2014-07-24 01:59:36

Riven (10 views)
2014-07-23 21:16:32

Riven (11 views)
2014-07-23 21:07:15

Riven (12 views)
2014-07-23 20:56:16

ctomni231 (43 views)
2014-07-18 06:55:21

Zero Volt (38 views)
2014-07-17 23:47:54

danieldean (32 views)
2014-07-17 23:41:23

MustardPeter (34 views)
2014-07-16 23:30:00

Cero (50 views)
2014-07-16 00:42:17

Riven (50 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!