Java-Gaming.org Hi !
 Featured games (91) games approved by the League of Dukes Games in Showcase (757) Games in Android Showcase (229) games submitted by our members Games in WIP (844) 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 2735 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

 EgonOlsen (78 views) 2018-06-10 19:43:48 EgonOlsen (58 views) 2018-06-10 19:43:44 EgonOlsen (78 views) 2018-06-10 19:43:20 DesertCoockie (260 views) 2018-05-13 18:23:11 nelsongames (158 views) 2018-04-24 18:15:36 nelsongames (157 views) 2018-04-24 18:14:32 ivj94 (898 views) 2018-03-24 14:47:39 ivj94 (162 views) 2018-03-24 14:46:31 ivj94 (811 views) 2018-03-24 14:43:53 Solater (175 views) 2018-03-17 05:04:08
 Java Gaming Resourcesby philfrei2017-12-05 19:38:37Java Gaming Resourcesby philfrei2017-12-05 19:37:39Java Gaming Resourcesby philfrei2017-12-05 19:36:10Java Gaming Resourcesby philfrei2017-12-05 19:33:10List 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:05
 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