Giter Club home page Giter Club logo

gamedev-pythagoras's Introduction

This is the source Code for this guide


Functionality Documentation

The following program uses C# to calculate the distance between two points and the angle from the first point to the second point, based on the Pythagorean theorem.

This implementation is typically useful in the field of game development to find distances and angles between game objects or players in a 2D space.

Classes

Program Class

The main class of this application, which houses all the logic.

Fields

The class has four static float fields:

  • point1X and point1Y: These represent the x and y coordinates respectively of the first point.
  • point2X and point2Y: These represent the x and y coordinates respectively of the second point.

Main(string[] args) Method

This is the entry point of the application. It begins by accepting a string from the console which should contain the coordinates of two points. The points should be in the following format: "x1 y1 x2 y2", where x1 and y1 are the coordinates for the first point, and x2 and y2 are the coordinates for the second point.

It then calls the GetInputValuesFromString(string input) method to parse the input string and assign the coordinate values to the relevant fields.

Next, the program calculates the distance between the two points using the Pythagorean theorem:

float distance = (float) Math.Sqrt((Math.Pow(point1X - point2X, 2) + Math.Pow(point1Y - point2Y, 2)));

The angle from the first point to the second is calculated using the Math.Atan2 function and converted to degrees:

double angle = (float)Math.Atan2(point2Y - point1Y, point2X - point1X);
angle *= (float)180/Math.PI;

These values are then printed to the console.

The application will continue to accept new input from the console until the user inputs a string that starts with 'q', which will exit the application.

GetInputValuesFromString(string input) Method

This method is used to parse an input string and extract the coordinates for two points. The method assumes the input string is in the format: "x1 y1 x2 y2". It uses the IndexOf(' ') function to find the spaces in the string and the Substring function to extract the coordinate values.

Usage

To run this application, a user would input a string of four numbers separated by spaces, representing two 2D coordinates (e.g., "1 1 4 5"). The application will then output the distance between these two points and the angle from the first point to the second, rounded to six and five decimal places respectively.

The user can then input another set of coordinates or type 'q' to quit the application.

gamedev-pythagoras's People

Contributors

nawodyaishan avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.