public static Vector2 Right(Vector2 a, Vector2 b) { float rotation = (float)Math.Atan2(b.Y - a.Y, b.X - a.X); Vector2 direction = new Vector2((float)Math.Cos(rotation), (float)Math.Sin(rotation)); direction.Normalize(); return(AdvancedMath.Rotate(new Vector2(1, 1) * direction, 180)); }
private void DrawGridOld(int gridSize, int lineWidth) { for (int i = AdvancedMath.GetNearestMultiple((int)Math.Round(camera.ScreenToWorldSpace(new Vector2()).Y), gridSize); i < AdvancedMath.GetNearestMultiple((int)Math.Round(camera.ScreenToWorldSpace(new Vector2(0, Window.ClientBounds.Height)).Y), gridSize); i += gridSize) { DrawLine(_spriteBatch, new Vector2((int)Math.Round(camera.ScreenToWorldSpace(new Vector2()).X), (i)), new Vector2((int)Math.Round(camera.ScreenToWorldSpace(new Vector2(Window.ClientBounds.Width, 0)).X), (i)), lineWidth); lines++; } int vertical = 0; for (int i = AdvancedMath.GetNearestMultiple((int)Math.Round(camera.ScreenToWorldSpace(new Vector2()).X), gridSize); i < AdvancedMath.GetNearestMultiple((int)Math.Round(camera.ScreenToWorldSpace(new Vector2(Window.ClientBounds.Width, 0)).X), gridSize); i += gridSize) { DrawLine(_spriteBatch, new Vector2((i), (int)Math.Round(camera.ScreenToWorldSpace(new Vector2()).Y)), new Vector2((i), (int)Math.Round(camera.ScreenToWorldSpace(new Vector2(0, Window.ClientBounds.Height)).Y)), lineWidth); lines++; vertical++; } }
//public double ConvertToRadians(double angle) //Stulen från unity //{ // return (Math.PI / 180) * angle; //} public static Vector2 ClampMagnitude(Vector2 vector, float maxLength)//Stulen från unity { float sqrMagnitude = AdvancedMath.sqrMagnitude(vector); if (sqrMagnitude > maxLength * maxLength) { float mag = (float)Math.Sqrt(sqrMagnitude); //these intermediate variables force the intermediate result to be //of float precision. without this, the intermediate result can be of higher //precision, which changes behavior. float normalized_x = vector.X / mag; float normalized_y = vector.Y / mag; return(new Vector2(normalized_x * maxLength, normalized_y * maxLength)); } return(vector); }
protected override void Update(GameTime gameTime) { if (IsActive) { Input.GetState(true); if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) { Exit(); } for (int i = 0; i < inputButtons.Count; i++) { inputButtons[i].Item1.Selected(Window); if (inputButtons[i].Item1.Modified()) { try { List <Vector2> temp = new List <Vector2>(); //SetEquation(temp, -40, 80, inputButtons[i].Item1.text); DrawEquationOverScren(temp, inputButtons[i].Item1.text); inputButtons[i].Item2.Clear(); for (int a = 0; a < temp.Count; a++) { inputButtons[i].Item2.Add(temp[a]); } inputButtons[i].Item1.modifiedText = false; } catch (Exception e) { Debug.WriteLine("Failed to update graph: " + e.Message); inputButtons[i].Item2.Clear(); } } } if (AdvancedMath.Vector2Distance(lastUpdate, position) > 50 * 2) { for (int i = 0; i < inputButtons.Count; i++) { try { List <Vector2> temp = new List <Vector2>(); //SetEquation(temp, -40, 80, inputButtons[i].Item1.text); DrawEquationOverScren(temp, inputButtons[i].Item1.text); inputButtons[i].Item2.Clear(); for (int a = 0; a < temp.Count; a++) { inputButtons[i].Item2.Add(temp[a]); } inputButtons[i].Item1.modifiedText = false; } catch (Exception e) { Debug.WriteLine("Failed to update graph: " + e.Message); inputButtons[i].Item2.Clear(); } } lastUpdate = position; } // TODO: Add your update logic here camera.Zoom = (float)(Input.clampedScrollWheelValue * 0.001) + 1; camera.UpdateCamera(position); UpdateMouse(gameTime); } else { for (int i = 0; i < inputButtons.Count; i++) { if (inputButtons[i].Item1.Selected(Window)) { inputButtons[i].Item1.SetSelected(Window, false); } } Input.GetState(false); lastMousePosition = Input.MousePos(); } base.Update(gameTime); }
private void DrawGrid(int gridSize, int lineWidth) { //Horiziontal lines for (int i = AdvancedMath.GetNearestMultiple((int)Math.Round(camera.ScreenToWorldSpace(new Vector2()).Y), gridSize); i < AdvancedMath.GetNearestMultiple((int)Math.Round(camera.ScreenToWorldSpace(new Vector2(0, Window.ClientBounds.Height)).Y), gridSize) + gridSize; i += gridSize) { int myLineWidth = i / gridSize % 5 == 0 ? i / gridSize % 25 == 0 ? lineWidth * 2 : lineWidth : lineWidth / 2; if ((float)myLineWidth * (float)camera.Zoom > 0.9f) { DrawLine(_spriteBatch, new Vector2((int)Math.Round(camera.ScreenToWorldSpace(new Vector2(-10, 0)).X), (i)), new Vector2((int)Math.Round(camera.ScreenToWorldSpace(new Vector2(Window.ClientBounds.Width + 10, 0)).X), (i)), myLineWidth); lines++; } else { } } // Vertical lines int vertical = 0; for (int i = AdvancedMath.GetNearestMultiple((int)Math.Round(camera.ScreenToWorldSpace(new Vector2()).X), gridSize); i < AdvancedMath.GetNearestMultiple((int)Math.Round(camera.ScreenToWorldSpace(new Vector2(Window.ClientBounds.Width, 0)).X), gridSize) + gridSize; i += gridSize) { int myLineWidth = i / gridSize % 5 == 0 ? i / gridSize % 25 == 0 ? lineWidth * 2 : lineWidth : lineWidth / 2; if ((float)myLineWidth * (float)camera.Zoom > 0.9f) { DrawLine(_spriteBatch, new Vector2((i), (int)Math.Round(camera.ScreenToWorldSpace(new Vector2(0, -10)).Y)), new Vector2((i), (int)Math.Round(camera.ScreenToWorldSpace(new Vector2(0, Window.ClientBounds.Height + 10)).Y)), myLineWidth); lines++; vertical++; } else { } } }
protected override void Draw(GameTime gameTime) { lines = 0; GraphicsDevice.Clear(Color.White); //_spriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.PointClamp); _spriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.PointClamp, transformMatrix: camera.transform); int size = 50; int width = 5; DrawGrid(size, width); //for (int i = 0; i < Window.ClientBounds.Height; i += size) //{ // DrawLine(_spriteBatch, new Vector2(0, i), new Vector2(Window.ClientBounds.Width, i), width); // lines++; //} //for (int a = 0; a < Window.ClientBounds.Width; a += size) //{ // DrawLine(_spriteBatch, new Vector2(a, 0), new Vector2(a, Window.ClientBounds.Height), width); // lines++; //} List <Color> färger = new List <Color>(); färger.Add(Color.Red); färger.Add(Color.Brown); färger.Add(Color.CornflowerBlue); färger.Add(Color.Green); for (int a = 0; a < allEquations.Count; a++) { for (int i = 0; i < allEquations[a].Count; i++) { if (i + 1 < allEquations[a].Count) { DrawLine(_spriteBatch, allEquations[a][i], allEquations[a][i + 1], (int)(width * 1.5), a < färger.Count ? färger[a] : Color.Blue); } } } for (int i = 0; i < inputButtons.Count; i++) { for (int a = 0; a < inputButtons[i].Item2.Count; a++) { if (a + 1 < inputButtons[i].Item2.Count) { DrawLine(_spriteBatch, inputButtons[i].Item2[a], inputButtons[i].Item2[a + 1], (int)(width * 1.5), i < färger.Count ? färger[i] : Color.Blue); } } } _spriteBatch.Draw(square, new Rectangle(300, 300, 200, 200), Color.Red); //Vector2 right = AdvancedMath.Right(AdvancedMath.AngleBetween(new Vector2(), camera.ScreenToWorldSpace(Input.MousePos()))); //DrawLine(_spriteBatch, new Vector2(0, 0) * (5 * right), camera.ScreenToWorldSpace(Input.MousePos()) * (5 * right)); // TODO: Add your drawing code here Vector3 temp = camera.transform.Translation; //for (int i = ((int)Math.Round(camera.ScreenToWorldSpace(new Vector2()).Y) % size); i < ((int)Math.Round(camera.ScreenToWorldSpace(new Vector2(0, Window.ClientBounds.Width)).Y) % size); i += size) //{ // DrawLine(_spriteBatch, new Vector2((int)Math.Round(camera.ScreenToWorldSpace(new Vector2()).X), (i)), new Vector2((int)Math.Round(camera.ScreenToWorldSpace(new Vector2(Window.ClientBounds.Width, 0)).X), (i)), width); // lines++; //} //for (int a = 0; a < Window.ClientBounds.Width; a++) //{ // DrawLine(_spriteBatch, new Vector2((a + 1) * size, 0), new Vector2((a + 1) * size, Window.ClientBounds.Height), width); //} //for (float i = -width*4 - _graphics.PreferredBackBufferWidth * Math.Abs(position.X) / 100 * 30 - (Math.Abs(position.X) > -1 || Math.Abs(position.X) < 1 ? _graphics.PreferredBackBufferWidth : 0); i < _graphics.PreferredBackBufferWidth + position.X; i += 15)// for (float i = -120 - _graphics.PreferredBackBufferWidth * Math.Clamp(Math.Abs(position.X), 1, float.MaxValue) / 100 * 30 - (Math.Abs(position.X) > -1 || Math.Abs(position.X) < 1 ? _graphics.PreferredBackBufferWidth : 0); i < _graphics.PreferredBackBufferWidth + position.X; i += 15) //{ // _spriteBatch.Draw(gräsTile, new Vector2(i, ground), null, Color.White, 0, new Vector2(7.5f, -0), 2, SpriteEffects.None, 1); //} //for (float a = 0 + 30; a < _graphics.PreferredBackBufferHeight + _graphics.PreferredBackBufferHeight / 8; a += 30) //{ // for (float i = -120 - _graphics.PreferredBackBufferWidth * Math.Abs(position.X) / 100 * 30 - (Math.Abs(position.X) > -1 || Math.Abs(position.X) < 1 ? _graphics.PreferredBackBufferWidth : 0); i < _graphics.PreferredBackBufferWidth + position.X; i += 15) // for (float i = -120 - _graphics.PreferredBackBufferWidth * Math.Clamp(Math.Abs(position.X), 1, float.MaxValue) / 100 * 30 - (Math.Abs(position.X) > -1 || Math.Abs(position.X) < 1 ? _graphics.PreferredBackBufferWidth : 0); i < _graphics.PreferredBackBufferWidth + position.X; i += 15) // { // _spriteBatch.Draw(jordTile, new Vector2(i, a), null, Color.White, 0, new Vector2(7.5f, -0), 2, SpriteEffects.None, 1); // } //} _spriteBatch.End(); Vector2 offset = new Vector2(0, 300); _spriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.PointClamp); _spriteBatch.DrawString(font, "pos: " + position.ToString(), new Vector2(5, 5) + offset, Color.Red); _spriteBatch.DrawString(font, "mousePos: " + Input.MousePos().ToString(), new Vector2(5, 25) + offset, Color.Red); _spriteBatch.DrawString(font, "lines: " + lines.ToString(), new Vector2(5, 45) + offset, Color.Red); _spriteBatch.DrawString(font, "rest: " + (camera.ScreenToWorldSpace(new Vector2()).Y % width).ToString(), new Vector2(5, 65) + offset, Color.Red); _spriteBatch.DrawString(font, "nearest: " + (AdvancedMath.GetNearestMultiple((int)Math.Round(camera.ScreenToWorldSpace(new Vector2()).Y), size)).ToString(), new Vector2(5, 85) + offset, Color.Red); _spriteBatch.DrawString(font, "zoom: " + (camera.Zoom).ToString(), new Vector2(5, 105) + offset, Color.Red); for (int i = 0; i < inputButtons.Count; i++) { inputButtons[i].Item1.Draw(_spriteBatch, font); //for (int a = 0; a < inputButtons[i].Item2.Count; a++) //{ // if (a + 1 < inputButtons[i].Item2.Count) // { // DrawLine(_spriteBatch, inputButtons[i].Item2[a], inputButtons[i].Item2[a], (int)(width * 1.5), i < färger.Count ? färger[i] : Color.Blue); // } //} } _spriteBatch.End(); base.Draw(gameTime); }