Java-Gaming.org Hi !
 Featured games (84) games approved by the League of Dukes Games in Showcase (570) Games in Android Showcase (154) games submitted by our members Games in WIP (619) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 Drawing a 1-pixel outline for an ellipse  (Read 1040 times) 0 Members and 1 Guest are viewing this topic.
HeroesGraveDev

JGO Kernel

Medals: 354
Projects: 11
Exp: 3 years

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

 « Posted 2014-01-01 05: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 equation1 - 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^2sqrt((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-coordfor(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.

 Riven (24 views) 2015-04-16 10:48:47 Duke0200 (37 views) 2015-04-16 01:59:01 Fairy Tailz (28 views) 2015-04-14 20:13:12 Riven (30 views) 2015-04-12 21:36:37 bus hotdog (47 views) 2015-04-10 02:39:32 CopyableCougar4 (50 views) 2015-04-10 00:51:04 BurntPizza (49 views) 2015-04-06 22:06:58 ags1 (52 views) 2015-04-02 10:58:48 Riven (50 views) 2015-04-01 18:27:05 ags1 (68 views) 2015-03-31 10:55:12
 BurntPizza 24x theagentd 21x wessles 15x Rayvolution 12x kingroka123 11x 65K 11x alwex 10x KevinWorkman 9x kevglass 8x ra4king 8x phu004 8x Ecumene 7x chrislo27 7x Roquen 7x Hanksha 7x Olo 7x
 How to: JGO Wikiby Mac702015-02-17 20:56:162D Dynamic Lighting2015-01-01 20:25:42How do I start Java Game Development?by gouessej2014-12-27 19:41:21Resources for WIP gamesby kpars2014-12-18 10:26:14Understanding relations between setOrigin, setScale and setPosition in libGdx2014-10-09 22:35:00Definite guide to supporting multiple device resolutions on Android (2014)2014-10-02 22:36:02List of Learning Resources2014-08-16 10:40:00List of Learning Resources2014-08-05 19:33:27
 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