Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "fmt"
- "strconv"
- "bufio"
- "os"
- "strings"
- )
- // T = M B + C
- // Co-ordinates should be entered in X, Y format
- func input() string {
- reader := bufio.NewReader(os.Stdin)
- text, err := reader.ReadString('\n')
- if err != nil {
- fmt.Println("\n[!] Reading input failed, exiting...")
- os.Exit(3)
- }
- return text
- }
- func parseCoords (coords string) [2]float64 {
- splitCoords := strings.Split(coords, ",") // split coords into array
- intCoords := [2]float64{} // create empty int array
- intOne, err := strconv.ParseFloat(strings.TrimSpace(splitCoords[0]), 64) // convert first element of splitCoords to float
- intTwo, err := strconv.ParseFloat(strings.TrimSpace(splitCoords[1]), 64) // same thing but with second element
- if err != nil {
- fmt.Println("[!] Failed to convert one or more co-ordinates to floats, exiting...")
- os.Exit(3)
- }
- intCoords[0] = intOne // add integers to int array
- intCoords[1] = intTwo
- return intCoords // return int array
- }
- func formatFloat(num float64) string {
- formatted := strconv.FormatFloat(num, 'f', -1, 64)
- return formatted
- }
- func main() {
- fmt.Println("[*] Welcome to my (not nerdy) tele-science calculator!") // yeah, it is cool (and NOT nerdy)
- fmt.Printf("\n[*] Enter first set of input co-ordinates: ") // this block of code takes the first set of inputs and outputs from the user
- firstInCoords := parseCoords(input())
- fmt.Printf("[*] Enter first set of output co-ordinates: ")
- firstOutCoords := parseCoords(input())
- fmt.Printf("[*] Enter second set of output co-ordinates: ")
- secondOutCoords := parseCoords(input())
- fmt.Printf("\n[*] Caluclating offsets... ")
- xOffset := secondOutCoords[0]-firstOutCoords[0]
- yOffset := secondOutCoords[1]-firstOutCoords[1]
- fmt.Println("[DONE]")
- fmt.Printf("[*] Offsets: X=%s, Y=%s\n", formatFloat(xOffset), formatFloat(yOffset))
- fmt.Printf("\n[*] Caluclating constants... ")
- xConstant := firstOutCoords[0]-(firstInCoords[0]*xOffset)
- yConstant := firstOutCoords[1]-(firstInCoords[1]*yOffset)
- fmt.Println("[DONE]")
- fmt.Printf("[*] Constants: X=%s, Y=%s\n", formatFloat(xConstant), formatFloat(yConstant))
- fmt.Printf("[*] Calculator ready!\n\n")
- for {
- fmt.Printf("[*] Enter co-ordinates to reverse calculate: ")
- coords := parseCoords(input())
- fmt.Printf("[*] Running calculations in reverse... ")
- outputCoords := [2]float64{}
- outputCoords[0] = (coords[0]+(xConstant*(-1)))/xOffset
- outputCoords[1] = (coords[1]+(yConstant*(-1)))/yOffset
- fmt.Println("[DONE]")
- fmt.Printf("[*] Results: X=%s, Y=%s\n\n", formatFloat(outputCoords[0]), formatFloat(outputCoords[1]))
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement