ZuneでLine関数が無いので作ってみよう
■
点A(x1,y1)と点B(x2、y2)の線を引きたい場合、そのまま線を引く関数が無いので角度raと長さrを求めて、これを使ってラインを表現して見ます。
namespace LineTest { public class Game1 : Microsoft.Xna.Framework.Game { GraphicsDeviceManager graphics; SpriteBatch spriteBatch; private Texture2D linePoint; private Rectangle lineDrawArea = new Rectangle(); private Rectangle lineSrcArea = new Rectangle(); private Vector2 lineCenter = new Vector2(0, 0); private SpriteEffects lineSpEffect = new SpriteEffects(); public Game1() { graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; graphics.PreferredBackBufferWidth = 240; graphics.PreferredBackBufferHeight = 320; // Frame rate is 30 fps by default for Zune. TargetElapsedTime = TimeSpan.FromSeconds(1 / 30.0); } protected override void LoadContent() { spriteBatch = new SpriteBatch(GraphicsDevice); linePoint= Content.Load<Texture2D>("Point"); lineSrcArea.X = 0; lineSrcArea.Y = 0; lineSrcArea.Width = 1; lineSrcArea.Height = 1; lineDrawArea.Height = 1; } protected override void Update(GameTime gameTime) { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) this.Exit(); base.Update(gameTime); } protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); spriteBatch.Begin(); for (int i = 0; i < 320; i += 8) drawLine(0, 0, 240, i, Color.White); spriteBatch.End(); base.Draw(gameTime); } //------------------------------------------------------------------------------- //ラインを引く //------------------------------------------------------------------------------- private void drawLine(int x1, int y1, int x2, int y2, Color lineColor) { double r,ra; int x, y; x = x2 - x1; y = y2 - y1; r =Math.Sqrt(x * x + y * y); ra = (x > 0) ? Math.Asin(y / rr) : Math.PI - Math.Asin(y / rr); lineDrawArea.X = x1; lineDrawArea.Y = y1; lineDrawArea.Width = (int)r; spriteBatch.Draw(linePoint, lineDrawArea, lineSrcArea, lineColor, (float)ra, lineCenter, lineSpEffect, 0); } } }
ここで"point"という画像ファイルを参照していますが、これが1X1ピクセルの白いPNGファイルです。
linePoint = Content.Load<Texture2D>("Point");