Code Snippets #19 - FillCircle

Published 7/30/2013 by createdbyx in Code Snippets | Programming
Tags: ,

```/// <summary>
/// Fills a circle.
/// </summary>
/// <param name="image">
/// The destination image.
/// </param>
/// <param name="centerX">
/// The x center position of the circle.
/// </param>
/// <param name="centerY">
/// The y center position of the circle.
/// </param>
/// The radius of the circle.
/// </param>
/// <param name="color">
/// The color to use.
/// </param>
public static void FillCircle<T>(this GenericImage<T> image, int centerX, int centerY, int radius, T color)
{
// source -> http://www.dailyfreecode.com/code/fill-circle-scan-line-circle-fill-675.aspx
int x1;
int x2;

var counter = (centerY + radius);

for (var count = (centerY - radius); count <= counter; count++)
{
x1 = (int)(centerX + Math.Sqrt((radius * radius) - ((count - centerY) * (count - centerY))) + 0.5);
x2 = (int)(centerX - Math.Sqrt((radius * radius) - ((count - centerY) * (count - centerY))) + 0.5);

image.DrawLine(x1, count, x2, count, color);
}
}```

Unity 101 Tip #70 – 50 Tips in one!

Published 7/29/2013 by createdbyx in Unity
Tags: , ,

I came across this article while searching for a way of unit testing my unity GUI windows. A link to the article is available here 50 Tips for Working with Unity (Best Practices). It contains some very sound advice and is well worth the time to read through.

Code Snippets #18 - DrawCircle

Published 7/27/2013 by createdbyx in Code Snippets | Programming
Tags: ,

Source (converted from Java) -> http://rosettacode.org/wiki/Bitmap/Midpoint_circle_algorithm#Java

```/// <summary>
/// Draws a circle.
/// </summary>
/// <param name="image">
/// The destination image.
/// </param>
/// <param name="centerX">
/// The x center position of the circle.
/// </param>
/// <param name="centerY">
/// The y center position of the circle.
/// </param>
/// The radius of the circle.
/// </param>
/// <param name="color">
/// The color to use.
/// </param>
public static void DrawCircle<T>(this GenericImage<T> image, int centerX, int centerY, int radius, T color)
{
// source (converted from Java) -> http://rosettacode.org/wiki/Bitmap/Midpoint_circle_algorithm#Java
var d = (5 - radius * 4) / 4;
var x = 0;

do
{
// ensure index is in range before setting (depends on your image implementation)
// in this case we check if the pixel location is within the bounds of the image before setting the pixel
image[centerX + x, centerY + y] = color;
image[centerX + x, centerY - y] = color;
image[centerX - x, centerY + y] = color;
image[centerX - x, centerY - y] = color;
image[centerX + y, centerY + x] = color;
image[centerX + y, centerY - x] = color;
image[centerX - y, centerY + x] = color;
image[centerX - y, centerY - x] = color;
if (d < 0)
{
d += 2 * x + 1;
}
else
{
d += 2 * (x - y) + 1;
y--;
}
x++;
} while (x <= y);
}```

Code Snippets #17 - DrawLine

Published 7/25/2013 by createdbyx in Code Snippets | Programming
Tags: ,

Source (converted from C) -> http://rosettacode.org/wiki/Bitmap/Bresenham%27s_line_algorithm#C

```/// <summary>
/// Draws a line on an <see cref="GenericImage{T}"/>.
/// </summary>
/// <param name="image">
/// The destination image.
/// </param>
/// <param name="x1">
/// The x position for the start of the line.
/// </param>
/// <param name="y1">
/// The y position for the start of the line.
/// </param>
/// <param name="x2">
/// The x position for the end of the line.
/// </param>
/// <param name="y2">
/// The y position for the end of the line.
/// </param>
/// <param name="color">
/// The color that the line will be drawn with.
/// </param>
public static void DrawLine<T>(this GenericImage<T> image, int x1, int y1, int x2, int y2, T color)
{
// source (converted from C) -> http://rosettacode.org/wiki/Bitmap/Bresenham%27s_line_algorithm#C
int dx = Math.Abs(x2 - x1), sx = x1 < x2 ? 1 : -1;
int dy = Math.Abs(y2 - y1), sy = y1 < y2 ? 1 : -1;
int err = (dx > dy ? dx : -dy) / 2;

for (; ; )
{
image[x1, y1] = color;
if (x1 == x2 && y1 == y2) break;
int e2 = err;
if (e2 > -dx) { err -= dy; x1 += sx; }
if (e2 < dy) { err += dx; y1 += sy; }
}
}```

Unity 101 Tip #69 – Let it shine!

Published 7/25/2013 by createdbyx in Unity
Tags: , ,

Seems like a no brainer but setting up a empty prefab with one or more lights in it can allow you to quickly light up your test scene quick and easy with predictable results. It also helps when you want to keep the same consistent lighting modal from scene to scene. Simply change the lighting in the prefab and hit apply to update all scenes that use it.

Unity 101 Tip #68 – ShowNotification

Published 7/20/2013 by createdbyx in Unity
Tags: , ,

Ever wonder how some unity extensions show a notification overlay? Check out the EditorWindow.ShowNotification method.

Unity 101 Tip #67 – Get and set build order of scenes

Published 7/7/2013 by createdbyx in Unity | Programming
Tags: , ,

You can get access to the build order for scenes in your editor scripts by calling EditorBuildSettings.scenes. This allows you to control what scenes are included with the build through your editor code.

```    public class SceneDump
{
public static void Dump()
{
var parts = from part in EditorBuildSettings.scenes
select string.Format("{0} - {1}", part.enabled, part.path);

Debug.Log(string.Join("\r\n", parts.ToArray()));
}
}```

Created by: X

Just another personal website in this crazy online world

 Dean Lunz (aka Created by: X) Computer programming nerd, and tech geek. About Me -- Resume