Thursday, October 23, 2014

Finding a linear equation, given a point and a parallel


A question was posed about the following problem:
A line goes through the point \((-2, -4)\) and is parallel to the graph of the equation $y = -x + 5$. Find the equation of the line in either point-slope form or slope-intercept form.
First, we must understand the problem.

What are we asked to find?

The problem asks us to find an equation for a line, in either point-slope form or point-intercept form.

What are the conditions for a correct solution?
  1. Our new line must pass through the point $(-2, -4)$.
  2. Our new line must be parallel to the graph of the equation $y=-x+5$. 
Do we understand all of the terminology?

The slope-intercept form of a linear equation is $y=mx+b$, where $y$ is the dependent variable, $x$ is the independent variable, $m$ is the slope of the line, and $b$ is the y-intercept of the line.

The point-slope form of a linear equation is $y-y_1=m(x-x_1)$, where $y$, $x$, and $m$ are the same, and $(x_1, y_1)$ is any point that the line passes through.

Next, can we determine a strategy?  

We are given a point that our new line must pass through, $(-2, -4)$.  That suggests that the point-slope form will be the most accessible.  We should use the point we were given, and try to construct our new line using that point.

Continuing, we must execute the strategy.

We want to find a linear equation in point-slope form, $y-y_1=m(x-x_1)$.  We have a point, $(-2, -4)$.  Substituting for $x_1$ and $y_1$, we have $$y--4 = m(x--2)$$The double negative signs are distracting, so we can use the additive inverse property to simplify this equation to $$y+4=m(x+2)$$.

We still must find $m$, the slope of our new line.  We know that the new line must be parallel to $y=-x+5$.  That is, itself, a linear equation in slope-intercept form.  The slope ($m$) is $-1$ (because $-x = -1\times x$).  The $y$-intercept ($b$) is $5$.  Therefore, the graph of $y=-x+5$ is a line that passes through $(0,5)$ (its y-intercept), and which falls by $1$ unit for every $1$ unit that it runs to the right, as so:


Since two parallel lines have the same slope, our new line must have the same slope as that given line.  Therefore, we can take the $-1$ slope of the given equation and substitute for $m$ in our incomplete equation.  This gives us $$y+4=-1(x+2)$$which solves our problem.


Can we check our solution?

It is a simple matter to see that our new line is parallel to the given line.  It is left as an exercise to the reader to show that the line passes through $(-2,-4)$.  (Be vigiliant:  looking at the graph is not enough.)

Bonus chatter:  Can we also present the solution in slope-intercept form?

We can use the properties of operations and equality to convert any point-slope equation to slope-intercept form, including this one.

First, we apply the distributive property of multiplication over addition to the right side of the equation, in order to remove the parentheses.  $$y+4=-1(x+2)$$ becomes $$y+4=(-1\times x) + (-1\times2)$$which becomes $$y+4=-x-2$$
Next, we apply the subtractive property of equality, subtracting $4$ from both sides of the equation.   $$y+4=-x-2$$ becomes $$y+4-4=-x-2-4$$which becomes $$y=-x-6$$ which is in slope-intercept form.

Monday, August 13, 2012

Better Pixel Format Debugging for AForge.NET

When you call Apply() on an AForge.NET filter that doesn’t support the format of the image you’re manipulating, the ensuing exception doesn’t provide much guidance.
The typical UnsupportedImageFormatException warning dialog.
The typical UnsupportedImageFormatException warning.
This snippet gives a bit more, to kickstart the debugging process.  It provides an extension method on AForge.NET filters that you can use before you attempt to apply a filter; if Apply() would fail because of an UnsupportedImageFormatException, the assert will fail, but it will tell you the filter that is going to fail, the unsupported format that you're passing, and the formats that the filter does support.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
namespace AForge.Imaging.Filters {

    /// <summary>
    /// Provides utility methods to assist coding against the AForge.NET 
    /// Framework.
    /// </summary>
    public static class AForgeUtility {

        /// <summary>
        /// Makes a debug assertion that an image filter that implements 
        /// the <see cref="IFilterInformation"/> interface can 
        /// process an image with the specified <see cref="PixelFormat"/>.
        /// </summary>
        /// <param name="filterInfo">The filter under consideration.</param>
        /// <param name="format">The PixelFormat under consideration.</param>
        [Conditional("DEBUG")]
        public static void AssertCanApply(
            this IFilterInformation filterInfo, 
            PixelFormat format) {
            Debug.Assert(
                filterInfo.FormatTranslations.ContainsKey(format),
                string.Format("{0} cannot process an image " 
                    + "with the provided pixel format. Provided "
                    + "format: {1}. Accepted formats: {2}.",
                    filterInfo.GetType().Name,
                    format.ToString(),
                    string.Join( ", ", filterInfo.FormatTranslations.Keys)));
        }
    }
}

Call the method after you instantiate the filter and before you apply it, and it may just save you a trip to the documentation.

1
2
3
BradleyLocalThresholding filter = new BradleyLocalThresholding();
filter.AssertCanApply( image.PixelFormat );
filter.ApplyInPlace( image );

The "assertion failed" dialog using my snippet.