Xellez
Xellez
CC#
Created by Xellez on 12/18/2022 in #help
Dictionary Implementation, but with custom class as key
I have implemented a dictionary like this code file Now Dictionary.Add(10, "Washington"); will work, but what if I want the key to be the latitude and longitude of the city?
public class GeoLocation
{
private float _latitude, _longitude;

public GeoLocation(float latitude, float longitude) {
_latitude = latitude;
_longitude= longitude;
}
}
public class GeoLocation
{
private float _latitude, _longitude;

public GeoLocation(float latitude, float longitude) {
_latitude = latitude;
_longitude= longitude;
}
}
can I pass my class as my key or do I need to adjust my dictclass ?
14 replies
CC#
Created by Xellez on 12/14/2022 in #help
❔ Circle line-segment collision detection
Tried following this https://stackoverflow.com/questions/1073336/circle-line-segment-collision-detection-algorithm
//item is line can be horizontal or vertical
float dx = item.X2- item.Position.X;
float dy = item.Y2 - item.Position.Y;

float a = dx * dx + dy * dy;
float b = 2 * (dx * (item.Position.X - ball.Position.X) + dy * (item.Position.Y - ball.Position.Y));
float c = (item.Position.X - ball.Position.X) * (item.Position.X - ball.Position.X) + (item.Position.Y - ball.Position.Y) * (item.Position.Y - ball.Position.Y) - ball.Radius * ball.Radius;

float discriminant = b * b - 4 * a * c;
//item is line can be horizontal or vertical
float dx = item.X2- item.Position.X;
float dy = item.Y2 - item.Position.Y;

float a = dx * dx + dy * dy;
float b = 2 * (dx * (item.Position.X - ball.Position.X) + dy * (item.Position.Y - ball.Position.Y));
float c = (item.Position.X - ball.Position.X) * (item.Position.X - ball.Position.X) + (item.Position.Y - ball.Position.Y) * (item.Position.Y - ball.Position.Y) - ball.Radius * ball.Radius;

float discriminant = b * b - 4 * a * c;
I dont know if what I tried is accurate to what the link answer does.
2 replies
CC#
Created by Xellez on 12/13/2022 in #help
❔ Is this the right way to implement an Interface
interface IObstacles
{
void Draw(Graphics g);
}

class Redbox : IObstacles
{
Pen Pen = new Pen(Color.Red);

Random Random = new Random();
Position Position;

float width;
float height;

public Redbox(float x, float y)
{
Position = new Position(x, y);

width = Random.Next(30, 100);
height = Random.Next(30, 100);
}


public void Draw(Graphics g)
{
g.DrawRectangle(Pen, Position.X , Position.Y, width, height);
}
}

class Bluebox: IObstacles
{
Pen Pen = new Pen(Color.Blue);

Random Random = new Random();
Position Position;

float width;
float height;

public Bluebox(float x, float y)
{
Position = new Position(x, y);

width = Random.Next(30, 100);
height = Random.Next(30, 100);
}

public void Draw(Graphics g)
{
g.DrawRectangle(Pen, Position.X, Position.Y, width, height);
}

}

class VerticaLine : IObstacles
{
Pen Pen = new Pen(Color.Yellow);

Position Position;

float Radius;

public VerticaLine(float x, float y, float radius)
{
Position = new Position(x, y);
Radius = radius;
}

public void Draw(Graphics g)
{
g.DrawLine(Pen, Position.X, Position.Y, Position.X, Radius);
}
}

class HorizonaLine : IObstacles
{
Pen Pen = new Pen(Color.Green);

Position Position;

float Radius;

public HorizonaLine(float x, float y, float radius)
{
Position = new Position(x, y);
Radius = radius;
}

public void Draw(Graphics g)
{
g.DrawLine(Pen, Position.X, Position.Y, Radius, Position.Y);
}
}
interface IObstacles
{
void Draw(Graphics g);
}

class Redbox : IObstacles
{
Pen Pen = new Pen(Color.Red);

Random Random = new Random();
Position Position;

float width;
float height;

public Redbox(float x, float y)
{
Position = new Position(x, y);

width = Random.Next(30, 100);
height = Random.Next(30, 100);
}


public void Draw(Graphics g)
{
g.DrawRectangle(Pen, Position.X , Position.Y, width, height);
}
}

class Bluebox: IObstacles
{
Pen Pen = new Pen(Color.Blue);

Random Random = new Random();
Position Position;

float width;
float height;

public Bluebox(float x, float y)
{
Position = new Position(x, y);

width = Random.Next(30, 100);
height = Random.Next(30, 100);
}

public void Draw(Graphics g)
{
g.DrawRectangle(Pen, Position.X, Position.Y, width, height);
}

}

class VerticaLine : IObstacles
{
Pen Pen = new Pen(Color.Yellow);

Position Position;

float Radius;

public VerticaLine(float x, float y, float radius)
{
Position = new Position(x, y);
Radius = radius;
}

public void Draw(Graphics g)
{
g.DrawLine(Pen, Position.X, Position.Y, Position.X, Radius);
}
}

class HorizonaLine : IObstacles
{
Pen Pen = new Pen(Color.Green);

Position Position;

float Radius;

public HorizonaLine(float x, float y, float radius)
{
Position = new Position(x, y);
Radius = radius;
}

public void Draw(Graphics g)
{
g.DrawLine(Pen, Position.X, Position.Y, Radius, Position.Y);
}
}
42 replies
CC#
Created by Xellez on 12/8/2022 in #help
Wrong count for my inversions function
It is supposed to be 10 inversion count but I am getting 13
var array = new int[] { 1, 8, 2, 0, 4, 3, 7, 6, 5 };

int inv_count = 0;
for (int i = 0; i < array.Length - 1; i++) {
for (int j = i + 1; j < array.Length; j++)
{
if (array[i] > array[j])
inv_count++;
}
}
return inv_count;
}
var array = new int[] { 1, 8, 2, 0, 4, 3, 7, 6, 5 };

int inv_count = 0;
for (int i = 0; i < array.Length - 1; i++) {
for (int j = i + 1; j < array.Length; j++)
{
if (array[i] > array[j])
inv_count++;
}
}
return inv_count;
}
What am I screwing up or missing something ?
9 replies