Solved

Polygon hatch fill in C

Posted on 2004-04-14
14
1,618 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
My eclipse editor won't start ? 4 144
C#, VS15, StructLayout 1 122
Problem to save 10 150
IIS Log files on Exchange 2013 server 6 85
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…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand recursion 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.

867 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

21 Experts available now in Live!

Get 1:1 Help Now