4 Sided Shape drawing function

The Partridge Family were neither partridges nor a family. Discuss.
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, 2:51 pm

Simple? No, not at all. If I tried to make something like this at the beginning, when I just started watching chili's tutorial, I wouldn't have had any idea how to even start on this. Chili has risen my motivation towards math, so I took out my dust books and when through it again, and now I can pretty much anything. Well, as long as it's fairly simple.

Making it wasn't too hard. I didn't start it from complete scratch. If you look at my first posts you can see that I at first had trouble doing the simplest geometrical calculations, but slowly gathered more information and quickly became better.

My best tip on making a function like this is:
1. Start by planing what you want to do.
2. Leave the maths out, and just think how you could do it. (kinda like vectors: to get to point B, I need to go from point A to point D, from there to C and then I can reach B)
3. Try to figure the math as best as you can.
4. The rest can "easily" be found online. Describe on google what you want to calculate, and chances are someone else has already asked it, and got an answer.
codinitup wrote:I would like to see the answer
Patience, you'll have to wait for it for a while ...Or still try to figure it out yourself. But yeah, I admit this might be an unfairly tricky challenge. Just don't lose your hopes, there's a Finnish saying "No one is a blacksmith at birth".
chili wrote:Pretty sweet lux.

Filling a convex polygon --
Thanks. It's not really a "polygon" or maybe I'm just not exactly sure what a polygon really is : -P
I used for the filling a similar method as in my new sight check stuff in the shooter game, but just inverted. Similar to your draw disc function, except that I just check using vector calculations if the point is on the right side of all lines, and then it obviously is inside the object. Also to speed it up, I use 3x3 pixels for it... It's fairly fast method, but nothing I would use in a huge ass 3d game. Maybe after some optimisation.
ʕ •ᴥ•ʔ

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, 2:56 pm

I guess there's something I can share:

Tada, faster DrawLine function. I was looking around the internet for faster methods and calculations than what chili uses. I stumbled across this *cant-remember-name* Algorithm for faster line drawing:

Code: Select all

void D3DGraphics::DrawLine3( int x1,int y1,int x2,int y2,int r,int g,int blu )
{
	int i, dx, dy, sdx, sdy, dxabs, dyabs, x, y, px, py;

	dx = x2 - x1; dy = y2 - y1;      
	dxabs = abs(dx); dyabs = abs(dy);

	// --- signum:
	if (dx > 0) {sdx = 1;} else if (dx < 0) {sdx = -1;} else {dx = 0;}
	if (dy > 0) {sdy = 1;} else if (dy < 0) {sdy = -1;} else {dy = 0;}
	// --- signum end

	x = dyabs >> 1; y = dxabs >> 1;
	px = x1; py = y1;

	if (dxabs >= dyabs) // the line is more horizontal than vertical
	{
		for(i = 0; i < dxabs; i++)
		{
			y += dyabs;
			if (y >= dxabs)
			{
				y -= dxabs;
				py += sdy;
			}
			px += sdx;
			PutPixel(px, py, r, g, blu);
		}
	}
	else // the line is more vertical than horizontal
	{
		for(i = 0; i < dyabs; i++)
		{
			x += dxabs;
			if (x >= dyabs)
			{
				x -= dyabs;
				px += sdx;
			}
			py += sdy;
			PutPixel(px, py, r, g, blu);
		}
	}
}
PS. couldn't get the signums working so I hand coded it : -/
ʕ •ᴥ•ʔ

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, 3:13 pm

Looks like the Bresenham algorithm to me. ;)

Polygon just means many-sided-shape, so yeah, those are polygons. Your algorithm seems decent enough. If you eventually wanna speed it up even more, you can look into scanline conversion. Or if you wait long enough I will cover it in the videos. :lol:

P.S. I heartily approve of LuX's not giving out the answer straight away. :lol:
Chili

User avatar
viruskiller
Posts: 399
Joined: June 14th, 2012, 5:07 pm

Re: 4 Sided Shape drawing function

Post by viruskiller » July 2nd, 2012, 6:00 pm

@ LuX hmm,i wonder since u have int corners,and double rad,why would u need first corner angle?,

i think u don't even need an angle,********************************************************************************************** ,and then u connect the "dots" :lol:

anyway if i misunderstood how your function works like then nvm what i've said.



i;ve dotted what is needed because i don't want it to be a spoiler:D
although if your function just draws a convex shape of Ncorners with all corners of same angle and all lines being equal then first corner is not needed unless something i think of is impossible to replicate in programming.

User avatar
Asimov
Posts: 814
Joined: May 19th, 2012, 11:38 pm

Re: 4 Sided Shape drawing function

Post by Asimov » July 2nd, 2012, 6:35 pm

Hi Lux,

I have to be honest I would have no idea how to make a filled polygon, let alone a 5 side shape, other than drawing lots of lines heh heh. I mean you can do that with a square box, but to do different shapes would be a nightmare.

In C# and XNA I used vectors, but not sure if that is possible in driectx using standard C++. I suppose it must be somewhere.

A 3 dimensional point could be shown with Vector(x,y,z); but z was backwards to 3ds max.

Then make it possible to import 3ds max models directly. Now I am dreaming LOL.

Asimov
----> Asimov
"You know no matter how much I think I have learnt. I always end up hitting brick walls"
http://www.asimoventerprises.co.uk

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, 6:45 pm

@viruskiller
The first corner is for rotation purposes, since I need to rotate the object.
But if you have an idea how its done, don't hesitate to share it here. The challenge is open for all.

@asimov
For filling I run a plane of pixels over the object, like in chili's DrawDisc, but insted of checking its distance, I calculate each "line" of the object to test if this location is on the right side of all lines. If it's outside of even one line it's not withing the object.




Also remember this function I made is just like any other object function in d3dgraphics chili made, it can be run as many times as you want. So keep the calculations "independant".

Here's another hint:
multicornerobj.png
multicornerobj.png (10.84 KiB) Viewed 3668 times
And here's and example : -D Using only this and a draw rectangle function.
example.png
example.png (11.96 KiB) Viewed 3668 times
ʕ •ᴥ•ʔ

User avatar
viruskiller
Posts: 399
Joined: June 14th, 2012, 5:07 pm

Re: 4 Sided Shape drawing function

Post by viruskiller » July 2nd, 2012, 7:51 pm

so i tought of 2 ways to skin the cat and u show me a third one ???? :lol:

edit:
i've kinda rushed on this reply,anyway i'm gona share the way i thought it can be done and the way i gues u did it.

my way:

struct xycoord
{
int x[100]
int y[100];
}

make a function that looks something like

xycoord perimeter points(int rad;int Ncorners)
{
in here some code that calculates the perimeter length of a circle with rad radius,

then split the length to Ncorners,and loop trough perimeter by the resulted length and return the x,y coords of the points.
}
then loop trough x,y and draw the lines.

}
now once i wrote my first idea it doesn't look that good anymore ,and that's the reason i think my other idea is the way u did it.

second way;

calculate the angle between the corner lines by spliting Nlines to 360, double Langle;
then taking xo,yo as the top of an isoscel triangle with it's sides beeing rad length,calculate the bottom line int Blength.

then draw a line starting at rad distance from xo,yo,parallel to the first angle base,
of length = Blength;

when at the end of the drawn line draw a new line of same length only this time shift the angle of the line to the right by Langle;
then draw again and then shift again.(to shift the angle i think u have to modify the m member of the draw line function.)

altough i think for this to work is needed a different draw line function that looks like this

gfx.DrawLine(int x,int y,int angle,int length); that can also return last x,y value for the pixel drawn.

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 pm

@viruskiller

Your second guess was close. I bet that'll work as well.
I won't be sharing the code yet, you can try to make it yourself, but the idea I used is this:

I have a x[100] and y[100] points for each corner and I divide PI * 2 by the amount of corners yo get the angle between all corners. Then I calculate from ox and oy a point using firstangleat and radius, giving me the first point. Then loop though the corner amount of points using the same method, always adding the c amount of angle, equal to the result of the angle per corner, to the original angle, firstangleat.

Basically FirstAngleAt + (AnglePerCorner * Corner)

Then just using the regular draw line function I pair up the corners, separately pairing the last and first corner.

@chili
I did it! Yesssssss... FUCK!

I get stack overflow when I try to fill objects of too big size, so I still need to fix that. But for small objects it works beautiful! Meanwhile; enjoy my simple paint bucket of doom, you can use your own images, but just use it to fill small to medium size objects, or it'll crash : -(

Click around what you want to fill, it will take a sample at your cursor position and fill the area of the same color with red....
Attachments
PaintBucket.rar
(40.59 KiB) Downloaded 151 times
ʕ •ᴥ•ʔ

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:56 pm

Actually never mind.... I put up my stack reserve, and works perfect. But that leaves me wondering... Can a function be heap allocated? If so, how?

Damn, spoke too fast. Well, I managed to make it fill a lot larger areas, about the size of 400 * 300, but that's about the cap of my stack, so I really need to allocate my nodes or something.
ʕ •ᴥ•ʔ

User avatar
npissoawsome
Posts: 114
Joined: June 8th, 2012, 3:01 pm

Re: 4 Sided Shape drawing function

Post by npissoawsome » July 3rd, 2012, 6:20 am

LuX wrote:Actually never mind.... I put up my stack reserve, and works perfect. But that leaves me wondering... Can a function be heap allocated? If so, how?

Damn, spoke too fast. Well, I managed to make it fill a lot larger areas, about the size of 400 * 300, but that's about the cap of my stack, so I really need to allocate my nodes or something.
Functions can't be heap allocated, why would you need them too?

Post Reply