Drawing 2D Lines as Rotated Quads

I haven’t had much time lately with work but one question I’ve seen asked many many times is how to draw lines of different widths. So, to cut to the chase I’ll share the code I’ve used to do it.

public void DrawLine(Vector3 p1, Color c1, Vector3 p2, Color c2, int width)
{
	float distance = Vector3.Distance(p1, p2);
	float halfDistance = distance / 2.0f;
	float halfWidth = width / 2.0f;

	Vector3 difference = p2 - p1;
	Vector3 destination = new Vector3(p1.X + difference.X / 2.0f, p1.Y + difference.Y / 2.0f, p1.Z + difference.Z);

	// Calculate angle between two points
	float angle = (float)Math.Atan2(difference.Y, difference.X);

	Vector3 v1, v2, v3, v4;

	v1 = new Vector3(-halfDistance, -halfWidth, 0); // Top Left
	v2 = new Vector3(halfDistance, -halfWidth, 0); // Top Right
	v3 = new Vector3(halfDistance, halfWidth, 0); // Bottom Right
	v4 = new Vector3(-halfDistance, halfWidth, 0); // Bottom Left

	Matrix m =
		Matrix.Identity *
		Matrix.CreateRotationZ(angle) *
		Matrix.CreateTranslation(destination);

	v1 = Vector3.Transform(v1, m);
	v2 = Vector3.Transform(v2, m);
	v3 = Vector3.Transform(v3, m);
	v4 = Vector3.Transform(v4, m);

	DrawQuad(v1, c1, v2, c2, v3, c2, v4, c1);
}

I’ve left a lot of fluff code out. I usually check if the line is a width of 1 and draw a normal line. I also left out the code on how to draw a quad as that can be found many other places already.

Let me google that for you

Ever read anything on a forum and thought, “I know that’s one of the first results in Google”. Well, then send your response with Let me google that for you. It’s a great way to let people know that google is smarter than you and him.

Paint Wars

I was listening to “.NET Rocks” show #414 this morning and there was an interview with Chris Marinos. While in college he wrote an Xna game that uses the WiiMote as an input device. He’s recently ported the code to F#. It sounded really interesting so go check it out.

One thing that was said in the interview that I didn’t really agree with though, Xna was said to be able to “run Xbox 360 games on your PC”. This isn’t right and it portrays Xna in the wrong way to me. Xna is Microsoft’s managed DirectX solution. Although it is right now geared more toward the game community, it can still be used for other purposes.

Jon Skeet

I just received my copy of C# In Depth by Jon Skeet. Jon has a great deal of knowledge on C# and if you’ve never ran into his blog I suggest you check it out here. I’ll post later more about the book but I’m pretty sure it’s a good one.

http://www.yoda.arachsys.com/csharp/

It Works

Ok, so the old laptop I’m working with isn’t ideal, but it does work and does run Xna Game Studio. I’ll probably only being doing 2D stuff on it. I did look into getting a bit more ram for it though as it only has 512 right now and most of that is used up just by the OS and the minimal apps running in the background.

Zend_Db_Table as a Model

In the Zend Framework, using Zend_Db_Table as your model class is not well advised. This practice can force you to put business logic in other places besides your model class. This may not be a big deal if your logic is simple but imagine the logic is fairly complicated. Now also imagine that the logic changes at some point. This can be a problem if you have this logic written in many different places.

There’s a solution to this problem though. You could write a Model class that uses Zend_Db_Table as a storage medium. Or, you can extend Zend_Db_Table to add your logic to the class. There is a way to simplify this process and that’s what this blog post is about.

More >