GithubHelp home page GithubHelp logo

Comments (6)

fabianfiorotto avatar fabianfiorotto commented on August 17, 2024

A little hack ... but I still think that represent the same object with different classes is a bad idea

require 'geometry'
module Geometry

 class Line

    def interception(line)
        if line.vertical? && self.vertial? then
            return nil
        end

        if line.vertical? then
            m = self.slope
            c = self.intercept
            x = self.first.x
            y = m * x + c
            return Point[x,y]
        end
        if self.vertical? then
            m = line.slope
            c = line.intercept
            x = line.first.x
            y = m * x + c
            return Point[x,y]
        end

        m1 = self.slope
        m2 = line.slope
        c1 = self.intercept
        c2 = line.intercept
        return nil if m1 == m2
        x = (c2 - c1) / (m1 - m2)
        y = m1 * x + c1
        return Point[x,y]
    end

 end

 class TwoPointLine

    def intercept(axis=:y)
            y1 = first.y ; y2 = last.y
            x1 = first.x ; x2 = last.x
        case axis
        when :x
            return - y1 * (x2 - x1 ) / ( y2 - y1) + x1
        when :y
             return - x1 * (y2 - y1 ) / ( x2 - x1) + y1
        end
    end

    def vertical?
        x1 = first.x ; x2 = last.x
        x1 == x2
    end

    def horizontal?
        y1 = first.y ; y2 = last.y
        y1 == y2
    end

 end

 #test
 if __FILE__ == $0 then
     line1 = Line[[1,1],[3,3]]
     line2 = Line[[1,3],[3,1]]
     #it should be Point[2,2] in both cases
     p line1.interception(line2)
     p line2.interception(line1)
 end
end

from geometry.

bfoz avatar bfoz commented on August 17, 2024

Thanks for looking into this. Line is implemented as a class cluster to avoid the conversion losses inherent in forcing all lines to be represented in the same way. Although, the current implementation does need a little cleaning.

I'm confused about what you're trying to implement here. Is this about adding an intersection method, or an intercept method?

from geometry.

fabianfiorotto avatar fabianfiorotto commented on August 17, 2024

I need the intercept in order to implement my intersection method. You can ignore it if you don't like it but it would be nice to have one. but intercept method should be available for all the instances because is a property of the line, no matter how it has been generated.

from geometry.

bfoz avatar bfoz commented on August 17, 2024

I've added an intercept method to all of the Line subclasses. Can you create a pull request for the intersection method?

from geometry.

fabianfiorotto avatar fabianfiorotto commented on August 17, 2024

Should this method return a point or an array of points? If you are thinking in extend it to interception with polygons in the future it should return an array.

BtW. Can I close this issue? I created it for the intercept method and it's done now.

from geometry.

bfoz avatar bfoz commented on August 17, 2024

I'm tempted to say it should return an Array, but Edge#intersection already returns a Point. Edge is more specialized (it was made for Polygon), so maybe that's ok. Either way, we can discuss this in another issue.

from geometry.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.