Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6024275
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:07:54+00:00 2026-05-23T04:07:54+00:00

I’m presently attempting to write a few classes to aid me in writing various

  • 0

I’m presently attempting to write a few classes to aid me in writing various applications and/or games that are primarily 2D. One of the things I really needed (since the C# XNA “Point” class sucks) was an IntVector2 class, which is meant to be very similar to the standard XNA “Vector2” class.

Presently, I have this coding (more stuff after the break):

using System;
using Microsoft.Xna.Framework;

namespace LPG.Utilities
{
    public class IntVector2
    {
        //Properties
        public readonly int X = 0;
        public readonly int Y = 0;
        private float magnitude = 0;

        //Special properties

        public float Magnitude
        {
            get
            {
                if (this.magnitude == 0)
                {
                    return (float)Math.Sqrt((Math.Abs(this.X ^ 2) + Math.Abs(this.Y ^ 2)));
                }
                else
                {
                    return this.magnitude;
                }
            }
        }

        //Methods

        public Vector2 Normalize()
        {
            return new Vector2(this.X / (float)this.Magnitude, this.Y / (float)this.Magnitude);
        }

        //Constructors

        public IntVector2()
        {
        }

        public IntVector2(int Both)
        {
            this.X = Both;
            this.Y = Both;
        }

        public IntVector2(int X, int Y)
        {
            this.X = X;
            this.Y = Y;
        }

        //Casts

        public static implicit operator Vector2(IntVector2 From)
        {
            return new Vector2(From.X, From.Y);
        }

        public static implicit operator IntVector2(Vector2 From)
        {
            return new IntVector2((int)From.X, (int)From.Y);
        }

        public static implicit operator string(IntVector2 From)
        {
            return "(" + From.X + ", " + From.Y + ")";
        }

        //IntVector2-IntVector2 operators

        public static IntVector2 operator +(IntVector2 First, IntVector2 Second)
        {
            return new IntVector2(First.X + Second.X, First.Y + Second.Y);
        }

        public static IntVector2 operator -(IntVector2 First, IntVector2 Second)
        {
            return new IntVector2(First.X - Second.X, First.Y - Second.Y);
        }

        public static IntVector2 operator *(IntVector2 First, IntVector2 Second)
        {
            return new IntVector2(First.X * Second.X, First.Y * Second.Y);
        }

        public static IntVector2 operator /(IntVector2 First, IntVector2 Second)
        {
            return new IntVector2(First.X / Second.X, First.Y / Second.Y);
        }

        //IntVector2-int operators:

        public static IntVector2 operator +(IntVector2 First, int Second)
        {
            return new IntVector2(First.X + Second, First.Y + Second);
        }

        public static IntVector2 operator -(IntVector2 First, int Second)
        {
            return new IntVector2(First.X - Second, First.Y - Second);
        }

        public static IntVector2 operator *(IntVector2 First, int Second)
        {
            return new IntVector2(First.X * Second, First.Y * Second);
        }

        public static IntVector2 operator /(IntVector2 First, int Second)
        {
            return new IntVector2(First.X / Second, First.Y / Second);
        }
    }
}

What should I do to improve this class? I thought about using a struct instead of a class, but I’m not quite experienced enough to make a real determination.

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T04:07:54+00:00Added an answer on May 23, 2026 at 4:07 am

    I would recommend a struct if you’re going to be newing it a lot, since then it won’t contribute to garbage collection, which if you’re developing on the xbox is a bit issue.

    Your magnitude method returns a float, no need to convert it when used in Normalize.

    You’re not storing the magnitude when you calculate it.

    You could add methods for dot and cross product, however not sure they’d be useful for integer vectors.

    You could create an override for ToString (great for when debugging/printing to screen etc.), as well as == Equals and GetHashCode.

    I’m not sure i like having an explicit conversion operator for string. I think that should be what ToString is for.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I am reading a book about Javascript and jQuery and using one of the
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I know there's a lot of other questions out there that deal with this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.