Java-Gaming.org Hi !
 Featured games (90) games approved by the League of Dukes Games in Showcase (731) Games in Android Showcase (217) games submitted by our members Games in WIP (798) 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 2254 times) 0 Members and 1 Guest are viewing this topic.
HeroesGraveDev

JGO Kernel

Medals: 382
Projects: 11
Exp: 4 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.

 Archive (337 views) 2017-04-27 17:45:51 buddyBro (535 views) 2017-04-05 03:38:00 CopyableCougar4 (979 views) 2017-03-24 15:39:42 theagentd (1015 views) 2017-03-24 15:32:08 Rule (993 views) 2017-03-19 12:43:22 Rule (973 views) 2017-03-19 12:42:17 Rule (972 views) 2017-03-19 12:36:21 theagentd (1071 views) 2017-03-16 05:07:07 theagentd (990 views) 2017-03-15 22:37:06 theagentd (761 views) 2017-03-15 22:32:18
 theagentd 13x orange451 13x philfrei 12x J0 10x Damocles 8x Coldstream24 7x Unimatrix325 7x KaiHH 7x Sickan 7x Guerra2442 7x Riven 7x 65K 4x Ecumene 4x VaTTeRGeR 4x Archive 4x Apo 4x
 List of Learning Resourcesby elect2017-03-13 14:05:44List of Learning Resourcesby elect2017-03-13 14:04:45SF/X Librariesby philfrei2017-03-02 08:45:19SF/X Librariesby philfrei2017-03-02 08:44:05SF/X Librariesby SkyAphid2017-03-02 06:38:56SF/X Librariesby SkyAphid2017-03-02 06:38:32SF/X Librariesby SkyAphid2017-03-02 06:38:05SF/X Librariesby SkyAphid2017-03-02 06:37:51
 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