xref: /petsc/src/sys/classes/draw/interface/drect.c (revision 27f49a208b01d2e827ab9db411a2d16003fe9262)
1 #include <petsc/private/drawimpl.h> /*I "petscdraw.h" I*/
2 
3 /*@C
4    PetscDrawIndicatorFunction - Draws an indicator function (where a relationship is true) on a `PetscDraw`
5 
6    Not Collective
7 
8    Input Parameters:
9 +  draw - a `PetscDraw`
10 .  xmin,xmax,ymin,ymax - region to draw indicator function
11 -  f - the indicator function
12 
13    Level: developer
14 
15 .seealso: `PetscDraw`
16 @*/
17 PetscErrorCode PetscDrawIndicatorFunction(PetscDraw draw, PetscReal xmin, PetscReal xmax, PetscReal ymin, PetscReal ymax, int c, PetscErrorCode (*indicator)(void *, PetscReal, PetscReal, PetscBool *), void *ctx)
18 {
19   int       i, j, xstart, ystart, xend, yend;
20   PetscReal x, y;
21   PetscBool isnull, flg;
22 
23   PetscFunctionBegin;
24   PetscValidHeaderSpecific(draw, PETSC_DRAW_CLASSID, 1);
25   PetscCall(PetscDrawIsNull(draw, &isnull));
26   if (isnull) PetscFunctionReturn(PETSC_SUCCESS);
27 
28   PetscCall(PetscDrawCoordinateToPixel(draw, xmin, ymin, &xstart, &ystart));
29   PetscCall(PetscDrawCoordinateToPixel(draw, xmax, ymax, &xend, &yend));
30   if (yend < ystart) {
31     PetscInt tmp = ystart;
32     ystart       = yend;
33     yend         = tmp;
34   }
35 
36   for (i = xstart; i <= xend; i++) {
37     for (j = ystart; j <= yend; j++) {
38       PetscCall(PetscDrawPixelToCoordinate(draw, i, j, &x, &y));
39       PetscCall(indicator(ctx, x, y, &flg));
40       if (flg) PetscCall(PetscDrawPointPixel(draw, i, j, c));
41     }
42   }
43   PetscFunctionReturn(PETSC_SUCCESS);
44 }
45 
46 /*@C
47    PetscDrawCoordinateToPixel - given a coordinate in a `PetscDraw` returns the pixel location
48 
49    Not Collective
50 
51    Input Parameters:
52 +  draw - the draw where the coordinates are defined
53 .  x - the horizontal coordinate
54 -  y - the vertical coordinate
55 
56    Output Parameters:
57 +  i - the horizontal pixel location
58 -  j - the vertical pixel location
59 
60    Level: developer
61 
62 .seealso: `PetscDraw`
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   PetscUseTypeMethod(draw, coordinatetopixel, x, y, i, j);
69   PetscFunctionReturn(PETSC_SUCCESS);
70 }
71 
72 /*@C
73    PetscDrawPixelToCoordinate - given a pixel in a `PetscDraw` returns the coordinate
74 
75    Not Collective
76 
77    Input Parameters:
78 +  draw - the draw where the coordinates are defined
79 .  i - the horizontal pixel location
80 -  j - the vertical pixel location
81 
82    Output Parameters:
83 +  x - the horizontal coordinate
84 -  y - the vertical coordinate
85 
86    Level: developer
87 
88 .seealso: `PetscDraw`
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   PetscUseTypeMethod(draw, pixeltocoordinate, i, j, x, y);
95   PetscFunctionReturn(PETSC_SUCCESS);
96 }
97 
98 /*@
99    PetscDrawRectangle - draws a rectangle  onto a drawable.
100 
101    Not Collective
102 
103    Input Parameters:
104 +  draw - the drawing context
105 .  xl,yl,xr,yr - the coordinates of the lower left, upper right corners
106 -  c1,c2,c3,c4 - the colors of the four corners in counter clockwise order
107 
108    Level: beginner
109 
110 .seealso: `PetscDraw`, `PetscDrawLine()`, `PetscDrawRectangle()`, `PetscDrawTriangle()`, `PetscDrawEllipse()`,
111           `PetscDrawMarker()`, `PetscDrawPoint()`, `PetscDrawString()`, `PetscDrawPoint()`, `PetscDrawArrow()`
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   PetscUseTypeMethod(draw, rectangle, xl, yl, xr, yr, c1, c2, c3, c4);
118   PetscFunctionReturn(PETSC_SUCCESS);
119 }
120