 # Trouble creating a type to be a vector of another defined type

So what I want to do is something like: Define a type Point that is constructed from floats passed by the user. This is supposed to store the coordinates of a point.
struct Point
x::Float64
y::Float64
end
This works well enough.

Now, I want to define a line segment that is defined as a vector of Point types which accepts two points from the user and creates a vector of 1000 points in between and assigns it to the line and constructs it.
s=Point(1,1)
t=Point(8,9)

``````struct Line::Vector{Point}
s::Point
t::Point
Line(s,t)=new([Point(s.x+i*(t.x-s.x)/1000,s.y+i*(t.y-s.y)/1000) for i=0:1000])
end
``````

I can’t quite get it to work, trying variations of the code above. Could someone please guide me in the right direction?

Yeah. I think a clearing up a few concepts will help.

What information do you want to store to represent the line? Do you want to store 1000 points? Or just the two end points? If youwant to store 1000 points, then you can have a method to create these 1000 points from two points input. The two points u have supplied need not be stored.

I suggest you don’t need to store 1000 points. You can just two the two end points and generate the points in between like this:

``````struct Point
x::Float64
y::Float64
end

struct Line
s::Point
t::Point
end

function Base.getindex(l::Line, i::Integer)
@assert 0 <= i <= 1000
Point(l.s.x + (l.t.x - l.s.x)/1000*i, l.s.y + (l.t.y - l.s.y)/1000*i)
end

function Base.size(::Line)
1000
end

l = Line(Point(0, 0), Point(1, 1))

l
l
l
``````
4 Likes

Thank you so much! This actually works well and fulfills what I was intending.

Just a thought though, what would you suggest if I wanted to create a Line type which would be a vector of Points that could take two points and then initialize itself? Is it possible to that? I’m just new to Julia, so please don’t mind my ignorance Not sure if you need a Line type for that. What about just

``````function makeline(p1, p2)

xrange = range(p1.x, stop=p2.x, length=1000)

yrange = range(p1.y, stop=p2.y, length=1000)

[Point(x, y) for (x, y) in zip(xrange, yrange)]

end

makeline(Point(0, 0), Point(1, 1))
``````
2 Likes