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