th94nn
asked on
Polygon hatch fill in C
Hi, I am looking for a function in C that will hatch a polygon with lines of a given angle and distance apart, can anyone point me in the right direction? thanks...
Windows GDI functions useful for Your task
CreatePolygonRgn
SelectClipRgn
CreateCompatibleBitmap
MoveToEx
LineTo
BitBlt
CreatePolygonRgn
SelectClipRgn
CreateCompatibleBitmap
MoveToEx
LineTo
BitBlt
If you are using Borland's compiler and you have graphics.h
Have a look as setfillstyle().It has the Hatch option.
Have a look as setfillstyle().It has the Hatch option.
ASKER
joghurt I am looking for an algorithm, best would be implemented in C, target systems are unix and Windows..
Just to be clear: Do you want to draw lines yourself into a given polygon?
You can try this
{
POINT pts[10];
HRGN hrg;
HDC dc, mdc;
HBITMAP hbmp, hbmpo;
int i;
int dist;
int dx;
pts[0].x = 10;
pts[0].y = 210;
pts[1].x = 110;
pts[1].y = 50;
pts[2].x = 210;
pts[2].y = 210;
pts[3].x = 110;
pts[3].y = 150;
pts[4].x = 10;
pts[4].y = 210;
dc = GetDC(Handle);
mdc = CreateCompatibleDC(dc);
hbmp = CreateCompatibleBitmap(dc, 300,300);
hbmpo = SelectObject(mdc,hbmp);
Rectangle(mdc,0,0,300,300) ;
// hatch parameters
dist = 15;
dx = -100;
for( i = 0 ; i < 50 ; i++)
{
MoveToEx(mdc,dx+dist*i,0,N ULL);
LineTo(mdc,dist*i,300);
}
hrg = CreatePolygonRgn(pts,4,ALT ERNATE);
SelectClipRgn(dc,hrg);
BitBlt(dc,0,0,300,300,mdc, 0,0,SRCCOP Y);
// cleanup
SelectClipRgn(dc,NULL);
DeleteObject(hrg);
SelectObject(mdc,hbmpo);
DeleteObject(hbmp);
ReleaseDC(Handle,dc);
DeleteDC(mdc);
}
{
POINT pts[10];
HRGN hrg;
HDC dc, mdc;
HBITMAP hbmp, hbmpo;
int i;
int dist;
int dx;
pts[0].x = 10;
pts[0].y = 210;
pts[1].x = 110;
pts[1].y = 50;
pts[2].x = 210;
pts[2].y = 210;
pts[3].x = 110;
pts[3].y = 150;
pts[4].x = 10;
pts[4].y = 210;
dc = GetDC(Handle);
mdc = CreateCompatibleDC(dc);
hbmp = CreateCompatibleBitmap(dc,
hbmpo = SelectObject(mdc,hbmp);
Rectangle(mdc,0,0,300,300)
// hatch parameters
dist = 15;
dx = -100;
for( i = 0 ; i < 50 ; i++)
{
MoveToEx(mdc,dx+dist*i,0,N
LineTo(mdc,dist*i,300);
}
hrg = CreatePolygonRgn(pts,4,ALT
SelectClipRgn(dc,hrg);
BitBlt(dc,0,0,300,300,mdc,
// cleanup
SelectClipRgn(dc,NULL);
DeleteObject(hrg);
SelectObject(mdc,hbmpo);
DeleteObject(hbmp);
ReleaseDC(Handle,dc);
DeleteDC(mdc);
}
ASKER
Yes that's right,
the function would call a line(x1,y1,x2,y2) to hatch the polygon,
function call could look like this:
hatch(polystack ,item_count ,origin_point ,dirct_point ,spacing)...
does this help?
the function would call a line(x1,y1,x2,y2) to hatch the polygon,
function call could look like this:
hatch(polystack ,item_count ,origin_point ,dirct_point ,spacing)...
does this help?
Here's how to roll your own:
1. Learn how to raster-fill a polygon (this is a fundamental computer graphics algorithm, in all sorts of books and websites).
2. Take your polygon, and transform (rotate) copies of all the vertices in it so that your desired hatch lines would be horizontal.
3. Use your rasterizing algorithm to compute horizontal hatch line segments (at the appropriate intervals) for the transformed polygon.
4. Using the inverse of the transform you used before, transform the endpoints of your line segments back to the original coordinate system.
Voila!
1. Learn how to raster-fill a polygon (this is a fundamental computer graphics algorithm, in all sorts of books and websites).
2. Take your polygon, and transform (rotate) copies of all the vertices in it so that your desired hatch lines would be horizontal.
3. Use your rasterizing algorithm to compute horizontal hatch line segments (at the appropriate intervals) for the transformed polygon.
4. Using the inverse of the transform you used before, transform the endpoints of your line segments back to the original coordinate system.
Voila!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
joghurt:
Getting there.., I had a look at the G.K.S libs. The gkfilc.f fortran subroutine is what I am looking for but in C, I think it would take for ever to translate it. I realy do need a robust concave/convex polygon hatch function in C, is there nothing out there?
NovaDenizen:
I need to hatch with vectors.
Getting there.., I had a look at the G.K.S libs. The gkfilc.f fortran subroutine is what I am looking for but in C, I think it would take for ever to translate it. I realy do need a robust concave/convex polygon hatch function in C, is there nothing out there?
NovaDenizen:
I need to hatch with vectors.
There are thousands of hatch fill implementations in C out there, I think. Just Google a bit if you don't want to write it yourself. However, the algorithm I wrote above is robust and it works for any (convex/concave) polygons and it's quite simple to do.
ASKER
I've been searching for quite a long time with not much success (GKS was the closest).. have you any links?
I have found a cross hatch function in a book, but it doesn't seem to work, should I post this?
could you have a look?
I have found a cross hatch function in a book, but it doesn't seem to work, should I post this?
could you have a look?
If it's less than some megabytes, just post it and we'll take a look.
ASKER
As I was getting the source ready, I spotted a typo error, the function works fine, with one small reserve, if the hatch line passes through the end point of two connecting vectors then the hatch function fails, any ideas? should I stil post the source?,
many thanks "joghurt" & co. for the moral & tech support. I got 90% what I want!
Respect....
many thanks "joghurt" & co. for the moral & tech support. I got 90% what I want!
Respect....
Or are you simply looking for an algorithm?