Advertisement
Guest User

Space Station 13 (SS13) Tele-Science Calculator

a guest
Apr 26th, 2019
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 2.65 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "fmt"
  5.     "strconv"
  6.     "bufio"
  7.     "os"
  8.     "strings"
  9. )
  10.  
  11. // T = M B + C
  12. // Co-ordinates should be entered in X, Y format
  13. // COMPILE IT FOR YOURSELVES NERDS
  14.  
  15. func input() string {
  16.     reader := bufio.NewReader(os.Stdin)
  17.     text, err := reader.ReadString('\n')
  18.     if err != nil {
  19.         fmt.Println("\n[!] Reading input failed, exiting...")
  20.         os.Exit(3)
  21.     }
  22.     return text
  23. }
  24.  
  25. func parseCoords (coords string) [2]float64 {
  26.     splitCoords := strings.Split(coords, ",") // split coords into array
  27.     intCoords := [2]float64{} // create empty int array
  28.     intOne, err := strconv.ParseFloat(strings.TrimSpace(splitCoords[0]), 64) // convert first element of splitCoords to float
  29.     intTwo, err := strconv.ParseFloat(strings.TrimSpace(splitCoords[1]), 64) // same thing but with second element
  30.     if err != nil {
  31.         fmt.Println("[!] Failed to convert one or more co-ordinates to floats, exiting...")
  32.         os.Exit(3)
  33.     }
  34.     intCoords[0] = intOne // add integers to int array
  35.     intCoords[1] = intTwo
  36.     return intCoords // return int array
  37. }
  38.  
  39. func formatFloat(num float64) string {
  40.     formatted := strconv.FormatFloat(num, 'f', -1, 64)
  41.     return formatted
  42. }
  43.  
  44. func main() {
  45.     fmt.Println("[*] Welcome to my (not nerdy) tele-science calculator!") // yeah, it is cool (and NOT nerdy)
  46.    
  47.     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
  48.     firstInCoords := parseCoords(input())
  49.     fmt.Printf("[*] Enter first set of output co-ordinates: ")
  50.     firstOutCoords := parseCoords(input())
  51.  
  52.     fmt.Printf("[*] Enter second set of output co-ordinates: ")
  53.     secondOutCoords := parseCoords(input())
  54.  
  55.     fmt.Printf("\n[*] Caluclating offsets... ")
  56.     xOffset := secondOutCoords[0]-firstOutCoords[0]
  57.     yOffset := secondOutCoords[1]-firstOutCoords[1]
  58.     fmt.Println("[DONE]")
  59.     fmt.Printf("[*] Offsets: X=%s, Y=%s\n", formatFloat(xOffset), formatFloat(yOffset))
  60.  
  61.     fmt.Printf("\n[*] Caluclating constants... ")
  62.     xConstant := firstOutCoords[0]-(firstInCoords[0]*xOffset)
  63.     yConstant := firstOutCoords[1]-(firstInCoords[1]*yOffset)
  64.     fmt.Println("[DONE]")
  65.     fmt.Printf("[*] Constants: X=%s, Y=%s\n", formatFloat(xConstant), formatFloat(yConstant))
  66.     fmt.Printf("[*] Calculator ready!\n\n")
  67.  
  68.     for {
  69.         fmt.Printf("[*] Enter co-ordinates to reverse calculate: ")
  70.         coords := parseCoords(input())
  71.         fmt.Printf("[*] Running calculations in reverse... ")
  72.         outputCoords := [2]float64{}
  73.         outputCoords[0] = (coords[0]+(xConstant*(-1)))/xOffset
  74.         outputCoords[1] = (coords[1]+(yConstant*(-1)))/yOffset
  75.         fmt.Println("[DONE]")
  76.         fmt.Printf("[*] Results: X=%s, Y=%s\n\n", formatFloat(outputCoords[0]), formatFloat(outputCoords[1]))
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement