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