C言語でグラフィックス | ||
【3】グラフィックス・ウィンドウでの入力 |
点座標の取得(GWcappnt)
◆サンプルプログラム 〔gr322.c〕
マウスポインタの点座標を調べる関数(GWcappnt)を使って取得できる値を確認する。
#include <GrWin.h>
#include <stdio.h>
int main(void)
{
int width = 640, height = 400;
int iro;
float mx, my;
GWopen(0);
GWsize(-5, &width, &height);
GWsize(-3, NULL, NULL);
GWvport(0.0, 0.0, (float)width / (float)height, 1.0);
GWindow(0.0, 0.0, (float)width - 1.0, (float)height - 1.0);
/* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= */
GWclear(-1);
GWsrect(0.0, 0.0, (float)width / 2.0, (float)height / 2.0,
13);
GWsrect((float)width / 2.0, 0.0, (float)width, (float)height
/ 2.0, 16);
GWsrect(0.0, (float)height / 2.0, (float)width / 2.0, 380.0,
14);
GWsrect((float)width / 2.0, (float)height / 2.0, (float)width,
380.0, 15);
GWsrect(0.0, 380.0, (float)width, (float)height, 18);
do{
iro = GWcappnt(&mx, &my,"ウィンドウ内の上部をクリックすると終了します。");
printf("%X(x=%5.2f y=%5.2f) ", iro,
mx, my);
}while(my < 380);
/* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= */
GWquit();
return 0;
}
int GWcappnt(float *X, float *Y, char *文字列); |
サンプルプログラムでは、塗りつぶしの四角形(GWsrect関数)で赤(13)、青(16)、緑(14)、黄(15)の四角形を描画している。Y座標380より上に水色(18)の四角形を描画して、この部分をクリックするとプログラムは終了する。
実行して、GWウィンドウ上でマウスを左クリックすると色番号と座標がコマンドプロンプトに表示される。
右クリックすると座標(0, 399)が得られるので、プログラムが終了する。
※※GWcappnt関数で取得できる色番号について※※
クリックした位置の色番号が返却されるはずであるが、違う値が返却されるようである。以後は、座標が取得できるだけとして扱うことにする。
線分座標の取得(GWcaplin)
◆サンプルプログラム 〔gr323.c〕
マウスをドラッグして始点と終点の座標を取得する関数(GWcaplin)を使って取得できる値を確認する。
#include <GrWin.h>
#include <stdio.h>
int main(void)
{
int width = 640, height = 400;
int ret;
float mx1, my1, mx2, my2;
GWopen(0);
GWsize(-5, &width, &height);
GWsize(-3, NULL, NULL);
GWvport(0.0, 0.0, (float)width / (float)height, 1.0);
GWindow(0.0, 0.0, (float)width - 1.0, (float)height - 1.0);
/* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= */
GWclear(-1);
GWsrect(0.0, 0.0, (float)width / 2.0, (float)height / 2.0,
13);
GWsrect((float)width / 2.0, 0.0, (float)width, (float)height
/ 2.0, 16);
GWsrect(0.0, (float)height / 2.0, (float)width / 2.0, 380.0,
14);
GWsrect((float)width / 2.0, (float)height / 2.0, (float)width,
380.0, 15);
GWsrect(0.0, 380.0, (float)width, (float)height, 18);
do{
ret = GWcaprect(&mx1, &my1, &mx2, &my2, "ウィンドウ内の上部からドラッグすると終了します。");
GWrect(mx1, my1, mx2, my2);
printf("%d(x1=%5.2f y1=%5.2f)-(x2=%5.2f
y2=%5.2f) ",ret,mx1,my1,mx2,my2);
}while(my1 < 380);
/* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= */
GWquit();
return 0;
}
int GWcaplin(float *X1, float *Y1, float *X2, float *Y2, char *文字列); |
実行して、GWウィンドウ上でマウスをドラッグすると返却値と座標がコマンドプロンプトに表示される。GWウィンドウ上にはドラッグした範囲に直線が描画される。
右クリックすると座標(0, 399)が得られるので、プログラムが終了する。
矩形座標の取得(GWcaprect)
◆サンプルプログラム 〔gr324.c〕
マウスをドラッグして矩形範囲の始点と終点の座標を取得する関数(GWcaprect)を使って取得できる値を確認する。
#include <GrWin.h> #include <stdio.h> int main(void) { int width = 640, height = 400; int ret; float mx1, my1, mx2, my2; GWopen(0); GWsize(-5, &width, &height); GWsize(-3, NULL, NULL); GWvport(0.0, 0.0, (float)width / (float)height, 1.0); GWindow(0.0, 0.0, (float)width - 1.0, (float)height - 1.0); /* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= */ GWclear(-1); GWsrect(0.0, 0.0, (float)width / 2.0, (float)height / 2.0, 13); GWsrect((float)width / 2.0, 0.0, (float)width, (float)height / 2.0, 16); GWsrect(0.0, (float)height / 2.0, (float)width / 2.0, 380.0, 14); GWsrect((float)width / 2.0, (float)height / 2.0, (float)width, 380.0, 15); GWsrect(0.0, 380.0, (float)width, (float)height, 18); do{ ret = GWcaprect(&mx1, &my1, &mx2, &my2, "ウィンドウ内の上部からドラッグすると終了します。"); GWrect(mx1, my1, mx2, my2); printf("%d(x1=%5.2f y1=%5.2f)-(x2=%5.2f y2=%5.2f) ",ret,mx1,my1,mx2,my2); }while(my1 < 380); /* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= */ GWquit(); return 0; }
int GWcaprect(float *X1, float *Y1, float *X2, float *Y2, char *文字列); |
実行して、GWウィンドウ上でマウスをドラッグした後、右クリックすると返却値と座標がコマンドプロンプトに表示される。GWウィンドウ上にはドラッグした範囲に四角形が描画される。
右クリックすると座標(0, 399)が得られるので、プログラムが終了する。
Copyright © 2005 Hiroshi Masuda |