xref: /petsc/src/sys/classes/draw/interface/drect.c (revision 030f984af8d8bb4c203755d35bded3c05b3d83ce)
1 
2 /*
3        Provides the calling sequences for all the basic PetscDraw routines.
4 */
5 #include <petsc/private/drawimpl.h>  /*I "petscdraw.h" I*/
6 
7 /*@C
8    PetscDrawIndicatorFunction - Draws an indicator function (where a relationship is true) on a PetscDraw
9 
10    Not collective
11 
12    Input Parameter:
13 +  draw - a PetscDraw
14 .  xmin,xmax,ymin,ymax - region to draw indicator function
15 -  f - the indicator function
16 
17    Level: developer
18 
19 @*/
20 PetscErrorCode PetscDrawIndicatorFunction(PetscDraw draw,PetscReal xmin,PetscReal xmax,PetscReal ymin,PetscReal ymax,int c,PetscErrorCode (*indicator)(void*,PetscReal,PetscReal,PetscBool*),void *ctx)
21 {
22   int            i,j,xstart,ystart,xend,yend;
23   PetscReal      x,y;
24   PetscBool      isnull,flg;
25   PetscErrorCode ierr;
26 
27   PetscFunctionBegin;
28   PetscValidHeaderSpecific(draw,PETSC_DRAW_CLASSID,1);
29   ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr);
30   if (isnull) PetscFunctionReturn(0);
31 
32   ierr = PetscDrawCoordinateToPixel(draw,xmin,ymin,&xstart,&ystart);CHKERRQ(ierr);
33   ierr = PetscDrawCoordinateToPixel(draw,xmax,ymax,&xend,&yend);CHKERRQ(ierr);
34   if (yend < ystart) { PetscInt tmp = ystart; ystart = yend; yend = tmp; }
35 
36   for (i=xstart; i<=xend; i++) {
37     for (j=ystart; j<=yend; j++) {
38       ierr = PetscDrawPixelToCoordinate(draw,i,j,&x,&y);CHKERRQ(ierr);
39       ierr = indicator(ctx,x,y,&flg);CHKERRQ(ierr);
40       if (flg) {
41         ierr = PetscDrawPointPixel(draw,i,j,c);CHKERRQ(ierr);
42       }
43     }
44   }
45   PetscFunctionReturn(0);
46 }
47 
48 /*@C
49    PetscDrawCoordinateToPixel - given a coordinate in a PetscDraw returns the pixel location
50 
51    Not collective
52 
53    Input Parameters:
54 +  draw - the draw where the coordinates are defined
55 -  x,y - the coordinate location
56 
57    Output Parameters:
58 -  i,j - the pixel location
59 
60    Level: developer
61 
62 @*/
63 PetscErrorCode PetscDrawCoordinateToPixel(PetscDraw draw,PetscReal x,PetscReal y,int *i,int *j)
64 {
65   PetscErrorCode ierr;
66 
67   PetscFunctionBegin;
68   PetscValidHeaderSpecific(draw,PETSC_DRAW_CLASSID,1);
69   if (!draw->ops->coordinatetopixel) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"This draw type %s does not support locating pixels",((PetscObject)draw)->type_name);
70   ierr = (*draw->ops->coordinatetopixel)(draw,x,y,i,j);CHKERRQ(ierr);
71   PetscFunctionReturn(0);
72 }
73 
74 /*@C
75    PetscDrawPixelToCoordinate - given a pixel in a PetscDraw returns the coordinate
76 
77    Not collective
78 
79    Input Parameters:
80 +  draw - the draw where the coordinates are defined
81 -  i,j - the pixel location
82 
83    Output Parameters:
84 .  x,y - the coordinate location
85 
86    Level: developer
87 
88 @*/
89 PetscErrorCode PetscDrawPixelToCoordinate(PetscDraw draw,int i,int j,PetscReal *x,PetscReal *y)
90 {
91   PetscErrorCode ierr;
92 
93   PetscFunctionBegin;
94   PetscValidHeaderSpecific(draw,PETSC_DRAW_CLASSID,1);
95   if (!draw->ops->pixeltocoordinate) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"This draw type %s does not support locating coordinates",((PetscObject)draw)->type_name);
96   ierr = (*draw->ops->pixeltocoordinate)(draw,i,j,x,y);CHKERRQ(ierr);
97   PetscFunctionReturn(0);
98 }
99 
100 /*@
101    PetscDrawRectangle - PetscDraws a rectangle  onto a drawable.
102 
103    Not Collective
104 
105    Input Parameters:
106 +  draw - the drawing context
107 .  xl,yl,xr,yr - the coordinates of the lower left, upper right corners
108 -  c1,c2,c3,c4 - the colors of the four corners in counter clockwise order
109 
110    Level: beginner
111 
112 .seealso: PetscDrawLine(), PetscDrawRectangle(), PetscDrawTriangle(), PetscDrawEllipse(),
113           PetscDrawMarker(), PetscDrawPoint(), PetscDrawString(), PetscDrawPoint(), PetscDrawArrow()
114 
115 @*/
116 PetscErrorCode  PetscDrawRectangle(PetscDraw draw,PetscReal xl,PetscReal yl,PetscReal xr,PetscReal yr,int c1,int c2,int c3,int c4)
117 {
118   PetscErrorCode ierr;
119 
120   PetscFunctionBegin;
121   PetscValidHeaderSpecific(draw,PETSC_DRAW_CLASSID,1);
122   if (!draw->ops->rectangle) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"This draw type %s does not support drawing rectangles",((PetscObject)draw)->type_name);
123   ierr = (*draw->ops->rectangle)(draw,xl,yl,xr,yr,c1,c2,c3,c4);CHKERRQ(ierr);
124   PetscFunctionReturn(0);
125 }
126