4 Sided Shape drawing function

The Partridge Family were neither partridges nor a family. Discuss.
User avatar
codinitup
Posts: 112
Joined: June 27th, 2012, 7:43 am

4 Sided Shape drawing function

Post by codinitup » July 2nd, 2012, 9:17 am

I know that it's very low tech, as in it doesn't make much progress, but I made a simple function that allows the user to draw a 4 sided shape with ease, in the long run it can save time though. :D
I'm hoping that somebody can get some use out of it. Anyways the code is posted below.

(P.S. The code can be manipulated to draw any 4 sided shape, however it is set to a rectangle right now in the code below)


Here's the code that belongs in the Game.cpp file

Code: Select all

Game::Game( HWND hWnd,const KeyboardServer& kServer,const MouseServer& mServer )
:	gfx( hWnd ),
	audio( hWnd ),
	kbd( kServer ),
	mouse( mServer ),
	x1( 400 ),
	y1( 300 ),
	x2( 450 ),
	y2( 300 ),
	x3( 450 ),
	y3( 310 ),
	x4( 400 ),
	y4( 310 ),
	r( 255 ),
	g( 255 ),
	b( 255 )
{}

void Game::Go()
{
	gfx.BeginFrame();
	ComposeFrame();
	gfx.EndFrame();
}

void Game::ComposeFrame()
{
	gfx.Draw4DShape(x1,y1,x2,y2,x3,y3,x4,y4,r,g,b);
}
And here is the D3DGraphics.h code

Code: Select all

	void Draw4SidedShape(int x1,int y1,int x2,int y2,int x3,int y3,int x4,int y4,int r,int g,int b);
And finally here is the D3DGraphics.cpp file code

Code: Select all

void D3DGraphics::Draw4SidedShape(int x1,int y1,int x2,int y2,int x3,int y3,int x4,int y4,int r,int g,int b)
{
	DrawLine(x1,y1,x2,y2,r,g,b);
	DrawLine(x2,y2,x3,y3,r,g,b);
	DrawLine(x3,y3,x4,y4,r,g,b);
	DrawLine(x4,y4,x1,y1,r,g,b);
}
MOOOOOO

User avatar
LuX
Posts: 1492
Joined: April 22nd, 2012, 12:33 pm
Location: Finland

Re: 4 Sided Shape drawing function

Post by LuX » July 2nd, 2012, 9:25 am

Cool, but have a look at this, its only the debug. Try to replicate it : -P
The function to draw the object has 10 lines of code (excluding drawline and filling function)
Test.rar
(57.6 KiB) Downloaded 189 times
LoL.png
(79.54 KiB) Downloaded 805 times
ʕ •ᴥ•ʔ

User avatar
codinitup
Posts: 112
Joined: June 27th, 2012, 7:43 am

Re: 4 Sided Shape drawing function

Post by codinitup » July 2nd, 2012, 9:35 am

Wow! Thats gunna be a real challenge! I was wondering if you had any pointers implemented in this?
MOOOOOO

User avatar
LuX
Posts: 1492
Joined: April 22nd, 2012, 12:33 pm
Location: Finland

Re: 4 Sided Shape drawing function

Post by LuX » July 2nd, 2012, 9:53 am

I take each corner point into memory, but I recalculate the points again for each frame.
To give a hint, here's how my void looks like: "void DrawMulti(int ox, int oy, double rad, int corners, double FirstCornerAtAngle, int r, int g, int b);"

I also made a rectangle function, similar to this, where you can set the midpoint, individually the height and length and the angle the rectangle gets drawn at. You can't access it, but in that debug there's also my tank test. This part was a test for tank parts, as I drew the tank using only objects that I made in d3dgraphics. So it has individually rotating body and turret, physics like a "real" tank (starting and stopping speed, etc.)
ʕ •ᴥ•ʔ

User avatar
codinitup
Posts: 112
Joined: June 27th, 2012, 7:43 am

Re: 4 Sided Shape drawing function

Post by codinitup » July 2nd, 2012, 9:57 am

That's actually pretty good! My idea was to create an algorithm that would check to see if all the points were connected somehow, and if they weren't, than draw a line so that they would always be connected.
MOOOOOO

User avatar
LuX
Posts: 1492
Joined: April 22nd, 2012, 12:33 pm
Location: Finland

Re: 4 Sided Shape drawing function

Post by LuX » July 2nd, 2012, 10:07 am

That kind of algorithms tend to be extremely slow, in this case you want to accurately calculate everything. If you're serious about trying to make a similar function, I'd recommend starting with just the frame, the filling function is simple too, but you'll need to know vectors for it. It's not like I drew the object a billion times in itself : -D That would be frame costly and the result would be a glitchy mess.

Ask for more help or hints, if you need any.
ʕ •ᴥ•ʔ

User avatar
codinitup
Posts: 112
Joined: June 27th, 2012, 7:43 am

Re: 4 Sided Shape drawing function

Post by codinitup » July 2nd, 2012, 10:14 am

Yeah, I've only watched up to his video on strutcs, so I'm wondering if I can do it with what he has taught me so far?
MOOOOOO

User avatar
LuX
Posts: 1492
Joined: April 22nd, 2012, 12:33 pm
Location: Finland

Re: 4 Sided Shape drawing function

Post by LuX » July 2nd, 2012, 10:23 am

Chili's teachings wont help you much here. I understood you have some experience in c++, that should be enough. You'll just need some imagination and skills in math.

But if you'll come up with an idea how you could draw it, or in this case calculate it, I can help with the maths part. If anything you'll see the function at least by the time I start showing my tank project.

If you already haven't, download my space shooter game "vortxnova 1.0" And have a look how I drew the character. I used a similar idea in this, but only a lot more flexible and automated.
ʕ •ᴥ•ʔ

User avatar
codinitup
Posts: 112
Joined: June 27th, 2012, 7:43 am

Re: 4 Sided Shape drawing function

Post by codinitup » July 2nd, 2012, 10:32 am

I give up lol. I think this is one challenge that has me stumped! I would like to see the answer, I'm sure it's something very simple that I'm just seeing. Very good and complex challenge though, did you make this brain teaser yourself?
MOOOOOO

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: 4 Sided Shape drawing function

Post by chili » July 2nd, 2012, 1:09 pm

Pretty sweet lux. 8-)

Filling a convex polygon (especially a triangle) is one of the things you need to be able to do to implement your own 3D engine from scratch. If you can do that, and you can rotate and project points, then you basically have a simple 3D engine.
Chili

Post Reply