掲示板システム
ホーム
アクセス解析
カテゴリ
ログアウト
座標の求め方 (ID:12604)
名前
ホームページ(ブログ、Twitterなど)のURL (省略可)
本文
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Edit1: TEdit; procedure FormMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); procedure FormMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); private { Private 宣言 } public { Public 宣言 } MouseDownXY : TPoint; end; var Form1: TForm1; implementation {$R *.dfm} type XY_Rect = record TopLeft , TopRight , BottomLeft , BottomRight : TPoint; end; // Canvasは、上に原点があるので、出力時に混同しないように。 // 数字で考えましょう。 // 左上(x1,y1)、左下(x2、y2)、右下(x3,y3)、右上(x4,y4) function SampleRect(X1,Y1,X2,Y2 ,Haba: integer) : XY_Rect; var R : TRect; sin,cos :real; begin Result.TopLeft := Point(X1,Y1); Result.BottomLeft := Point(X2,Y2); sin := (x2-x1) / sqrt(sqr(x1-x2)+sqr(y1-y2)); cos := (y1-y2) / sqrt(sqr(x1-x2)+sqr(y1-y2)); Result.BottomRight.X := trunc(x2+ cos * Haba); Result.BottomRight.Y := trunc(y2+ sin * Haba); Result.TopRight.X := trunc(x1+ cos * Haba); Result.TopRight.Y := trunc(y1+ sin * Haba); end; procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); begin MouseDownXY := Point(x,y); end; procedure TForm1.FormMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); var MouseXY : XY_Rect; begin with MouseXY do begin if (MouseDownXY.y<=y) then begin // 座標の上下関係代入。 TopLeft := Point(x,y); BottomLeft := MouseDownXY; end else begin TopLeft := MouseDownXY; BottomLeft := Point(x,y); end; MouseXY := SampleRect(TopLeft.x,TopLeft.y , BottomLeft.x,BottomLeft.y , StrToInt(Edit1.text)); // Haba + : 画面右に // Haba - : 画面左に Canvas.Polygon([TopLeft,BottomLeft,BottomRight,TopRight]); end; end; end.
←解決時は質問者本人がここをチェックしてください。
戻る
掲示板システム
Copyright 2021 Takeshi Okamoto All Rights Reserved.