xref: /petsc/src/sys/classes/draw/interface/drect.c (revision 5becb6a3a7fe6fe66ca978502f4679a4b24b29fc)
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 Parameters:
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 
26   PetscFunctionBegin;
27   PetscValidHeaderSpecific(draw,PETSC_DRAW_CLASSID,1);
28   PetscCall(PetscDrawIsNull(draw,&isnull));
29   if (isnull) PetscFunctionReturn(0);
30 
31   PetscCall(PetscDrawCoordinateToPixel(draw,xmin,ymin,&xstart,&ystart));
32   PetscCall(PetscDrawCoordinateToPixel(draw,xmax,ymax,&xend,&yend));
33   if (yend < ystart) { PetscInt tmp = ystart; ystart = yend; yend = tmp; }
34 
35   for (i=xstart; i<=xend; i++) {
36     for (j=ystart; j<=yend; j++) {
37       PetscCall(PetscDrawPixelToCoordinate(draw,i,j,&x,&y));
38       PetscCall(indicator(ctx,x,y,&flg));
39       if (flg) {
40         PetscCall(PetscDrawPointPixel(draw,i,j,c));
41       }
42     }
43   }
44   PetscFunctionReturn(0);
45 }
46 
47 /*@C
48    PetscDrawCoordinateToPixel - given a coordinate in a PetscDraw returns the pixel location
49 
50    Not collective
51 
52    Input Parameters:
53 +  draw - the draw where the coordinates are defined
54 .  x - the horizontal coordinate
55 -  y - the vertical coordinate
56 
57    Output Parameters:
58 +  i - the horizontal pixel location
59 -  j - the vertical pixel location
60 
61    Level: developer
62 
63 @*/
64 PetscErrorCode PetscDrawCoordinateToPixel(PetscDraw draw,PetscReal x,PetscReal y,int *i,int *j)
65 {
66   PetscFunctionBegin;
67   PetscValidHeaderSpecific(draw,PETSC_DRAW_CLASSID,1);
68   PetscCheck(draw->ops->coordinatetopixel,PETSC_COMM_SELF,PETSC_ERR_SUP,"This draw type %s does not support locating pixels",((PetscObject)draw)->type_name);
69   PetscCall((*draw->ops->coordinatetopixel)(draw,x,y,i,j));
70   PetscFunctionReturn(0);
71 }
72 
73 /*@C
74    PetscDrawPixelToCoordinate - given a pixel in a PetscDraw returns the coordinate
75 
76    Not collective
77 
78    Input Parameters:
79 +  draw - the draw where the coordinates are defined
80 .  i - the horizontal pixel location
81 -  j - the vertical pixel location
82 
83    Output Parameters:
84 +  x - the horizontal coordinate
85 -  y - the vertical coordinate
86 
87    Level: developer
88 
89 @*/
90 PetscErrorCode PetscDrawPixelToCoordinate(PetscDraw draw,int i,int j,PetscReal *x,PetscReal *y)
91 {
92   PetscFunctionBegin;
93   PetscValidHeaderSpecific(draw,PETSC_DRAW_CLASSID,1);
94   PetscCheck(draw->ops->pixeltocoordinate,PETSC_COMM_SELF,PETSC_ERR_SUP,"This draw type %s does not support locating coordinates",((PetscObject)draw)->type_name);
95   PetscCall((*draw->ops->pixeltocoordinate)(draw,i,j,x,y));
96   PetscFunctionReturn(0);
97 }
98 
99 /*@
100    PetscDrawRectangle - PetscDraws a rectangle  onto a drawable.
101 
102    Not Collective
103 
104    Input Parameters:
105 +  draw - the drawing context
106 .  xl,yl,xr,yr - the coordinates of the lower left, upper right corners
107 -  c1,c2,c3,c4 - the colors of the four corners in counter clockwise order
108 
109    Level: beginner
110 
111 .seealso: PetscDrawLine(), PetscDrawRectangle(), PetscDrawTriangle(), PetscDrawEllipse(),
112           PetscDrawMarker(), PetscDrawPoint(), PetscDrawString(), PetscDrawPoint(), PetscDrawArrow()
113 
114 @*/
115 PetscErrorCode  PetscDrawRectangle(PetscDraw draw,PetscReal xl,PetscReal yl,PetscReal xr,PetscReal yr,int c1,int c2,int c3,int c4)
116 {
117   PetscFunctionBegin;
118   PetscValidHeaderSpecific(draw,PETSC_DRAW_CLASSID,1);
119   PetscCheck(draw->ops->rectangle,PETSC_COMM_SELF,PETSC_ERR_SUP,"This draw type %s does not support drawing rectangles",((PetscObject)draw)->type_name);
120   PetscCall((*draw->ops->rectangle)(draw,xl,yl,xr,yr,c1,c2,c3,c4));
121   PetscFunctionReturn(0);
122 }
123