Solved

Polygon hatch fill in C

Posted on 2004-04-14
14
1,612 Views
Last Modified: 2007-12-19
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
Comment
Question by:th94nn
  • 5
  • 5
  • 2
  • +2
14 Comments
 
LVL 6

Expert Comment

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

Expert Comment

by:mokule
ID: 10828743
Windows GDI functions useful for Your task

CreatePolygonRgn
SelectClipRgn
CreateCompatibleBitmap
MoveToEx
LineTo
BitBlt
0
 
LVL 9

Expert Comment

by:ankuratvb
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

by:th94nn
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

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

Expert Comment

by:mokule
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

by:th94nn
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 22

Expert Comment

by:NovaDenizen
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

by:
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

by:th94nn
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?
NovaDenizen:
I need to hatch with vectors.
 
0
 
LVL 6

Expert Comment

by:joghurt
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

by:th94nn
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

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

Author Comment

by:th94nn
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now