Solved

# Polygon hatch fill in C

Posted on 2004-04-14
1,676 Views
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...
0
Question by:th94nn
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 5
• 5
• 2
• +2

LVL 6

Expert Comment

ID: 10827010
Operating system, programming environment?
Or are you simply looking for an algorithm?
0

LVL 17

Expert Comment

ID: 10828743

CreatePolygonRgn
SelectClipRgn
CreateCompatibleBitmap
MoveToEx
LineTo
BitBlt
0

LVL 9

Expert Comment

ID: 10829517
If you are using Borland's compiler and you have graphics.h

Have a look as setfillstyle().It has the Hatch option.
0

LVL 1

Author Comment

ID: 10830045
joghurt I am looking for an algorithm, best would be implemented in C, target systems are unix and Windows..
0

LVL 6

Expert Comment

ID: 10831228
Just to be clear: Do you want to draw lines yourself into a given polygon?
0

LVL 17

Expert Comment

ID: 10831447
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,NULL);
LineTo(mdc,dist*i,300);
}
hrg = CreatePolygonRgn(pts,4,ALTERNATE);
SelectClipRgn(dc,hrg);
BitBlt(dc,0,0,300,300,mdc,0,0,SRCCOPY);
// cleanup
SelectClipRgn(dc,NULL);
DeleteObject(hrg);
SelectObject(mdc,hbmpo);
DeleteObject(hbmp);
ReleaseDC(Handle,dc);
DeleteDC(mdc);
}
0

LVL 1

Author Comment

ID: 10831462
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?
0

LVL 22

Expert Comment

ID: 10836002
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!

0

LVL 6

Accepted Solution

joghurt earned 250 total points
ID: 10836014
This used to be one of the questions at the entry exam for the applicants at my previous working place. :-)

The basic algorithm would be the following:
1. Calculate the boundaries of the polygon according to the hatch direction. (It's simply the bounding rectangle in case of horizontal/vertical hatch lines but may be smaller for diagonal hatching, for example.)
2. Within these bounds move the current scanline according to the hatch direction.
3. Calculate intersections of the scanline and the polygon.
4. If you are between two intersections, draw the line, otherwise move.
5. Repeat steps 2, 3 and 4 while within the bounds.
0

LVL 1

Author Comment

ID: 10849580
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?
I need to hatch with vectors.

0

LVL 6

Expert Comment

ID: 10850276
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.
0

LVL 1

Author Comment

ID: 10856666
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?
0

LVL 6

Expert Comment

ID: 10858974
If it's less than some megabytes, just post it and we'll take a look.
0

LVL 1

Author Comment

ID: 10952189
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....
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

### Suggested Solutions

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were smallâ€¦
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and infâ€¦
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
###### Suggested Courses
Course of the Month2 days, 1 hour left to enroll