{"id":34214,"date":"2024-11-01T09:25:36","date_gmt":"2024-11-01T09:25:36","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=34214"},"modified":"2024-11-01T10:58:09","modified_gmt":"2024-11-01T10:58:09","slug":"c-coding-test-course-finding-the-direction-of-a-line-segment-2","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/34214\/","title":{"rendered":"C++ Coding Test Course, Finding the Direction of a Line Segment"},"content":{"rendered":"<p><body><\/p>\n<p>The coding test includes various problems to assess programming skills in the field. In this article, we will take a closer look at the process of solving the &#8216;Finding the Direction of a Line Segment&#8217; problem using C++. This problem is geometric in nature and requires a basic understanding of points and vectors.<\/p>\n<h2>Problem Description<\/h2>\n<p>Given points <code>A(x1, y1)<\/code>, <code>B(x2, y2)<\/code>, and <code>C(x3, y3)<\/code>, the task is to find the direction of the line segments <code>AB<\/code> and <code>AC<\/code>. The direction of the line segments can be classified as clockwise, counterclockwise, or collinear.<\/p>\n<h3>Input Format<\/h3>\n<pre><code>3\n0 0\n1 1\n2 2\n<\/code><\/pre>\n<p>The first line contains the number of points, followed by three lines, each containing the coordinates of a point.<\/p>\n<h3>Output Format<\/h3>\n<pre><code>0\n<\/code><\/pre>\n<p>0 indicates collinear, 1 indicates counterclockwise, and -1 indicates clockwise.<\/p>\n<h2>Algorithm Approach<\/h2>\n<p>Given points A, B, and C, their directionality can be determined using the cross product of the vectors. The sign of the cross product of the vector from A to B and the vector from A to C can be used to ascertain the direction.<\/p>\n<h3>Calculating Cross Product<\/h3>\n<p>The result obtained from the cross product is defined as follows:<\/p>\n<pre><code>cross_product = (x2 - x1) * (y3 - y1) - (y2 - y1) * (x3 - x1)\n<\/code><\/pre>\n<p>Here:<\/p>\n<ul>\n<li>cross_product &gt; 0 : Counterclockwise<\/li>\n<li>cross_product &lt; 0 : Clockwise<\/li>\n<li>cross_product = 0 : Collinear<\/li>\n<\/ul>\n<h2>Code Implementation<\/h2>\n<p>Now, let\u2019s write the code to solve the problem in C++.<\/p>\n<pre><code>#include &lt;iostream&gt;\n\nusing namespace std;\n\nint main() {\n    int x1, y1, x2, y2, x3, y3;\n    \n    \/\/ Input coordinates for points A, B, C\n    cout &lt;&lt; \"Enter coordinates for point A (x1 y1): \";\n    cin &gt;&gt; x1 &gt;&gt; y1;\n    cout &lt;&lt; \"Enter coordinates for point B (x2 y2): \";\n    cin &gt;&gt; x2 &gt;&gt; y2;\n    cout &lt;&lt; \"Enter coordinates for point C (x3 y3): \";\n    cin &gt;&gt; x3 &gt;&gt; y3;\n\n    \/\/ Calculate cross product\n    int cross_product = (x2 - x1) * (y3 - y1) - (y2 - y1) * (x3 - x1);\n    \n    \/\/ Determine direction\n    if (cross_product &gt; 0) {\n        cout &lt;&lt; \"Counterclockwise (1)\" &lt;&lt; endl;\n    } else if (cross_product &lt; 0) {\n        cout &lt;&lt; \"Clockwise (-1)\" &lt;&lt; endl;\n    } else {\n        cout &lt;&lt; \"Collinear (0)\" &lt;&lt; endl;\n    }\n\n    return 0;\n}\n<\/code><\/pre>\n<h2>Code Explanation<\/h2>\n<p>The code includes the following steps:<\/p>\n<ol>\n<li>It takes input for the coordinates of points A, B, and C from the user.<\/li>\n<li>It calculates the cross product to obtain <code>cross_product<\/code>.<\/li>\n<li>It determines and outputs the direction based on the result.<\/li>\n<\/ol>\n<h2>Additional Explanation for Deeper Understanding<\/h2>\n<p>This problem requires a geometric understanding. The cross product is generally used to distinguish the plane of two vectors in 3D space, but in 2D, it serves as a useful tool for determining direction. By understanding geometric concepts and vector mathematics, one can develop the ability to solve various problems.<\/p>\n<h3>Geometric Intuition<\/h3>\n<p>Geometrically, this problem also involves understanding the slope of a line. When moving from point A to B and from A to C, the relative slopes of the two vectors can be assessed to determine directionality. Depending on whether <code>cross_product<\/code> is positive or negative, one can ascertain if point C is to the left or right of line AB.<\/p>\n<h2>Conclusion<\/h2>\n<p>In this lecture, we covered a simple geometric problem that frequently appears in coding tests. The &#8216;Finding the Direction of a Line Segment&#8217; problem requires a fundamental understanding of geometry and can be solved using the cross product of vectors. By practicing such problems, one can cultivate algorithmic thinking and improve basic C++ programming skills.<\/p>\n<div class=\"note\">\n<strong>Note:<\/strong> This problem appears in various forms in real-world scenarios. For example, given N points, it can be extended into a polygon directionality determination problem. We encourage you to continue practicing geometric algorithms through such problems.\n<\/div>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The coding test includes various problems to assess programming skills in the field. In this article, we will take a closer look at the process of solving the &#8216;Finding the Direction of a Line Segment&#8217; problem using C++. This problem is geometric in nature and requires a basic understanding of points and vectors. Problem Description &hellip; <a href=\"https:\/\/atmokpo.com\/w\/34214\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;C++ Coding Test Course, Finding the Direction of a Line Segment&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[111],"tags":[],"class_list":["post-34214","post","type-post","status-publish","format-standard","hentry","category-c-coding-test-tutorials-2"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>C++ Coding Test Course, Finding the Direction of a Line Segment - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/atmokpo.com\/w\/34214\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"C++ Coding Test Course, Finding the Direction of a Line Segment - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"The coding test includes various problems to assess programming skills in the field. In this article, we will take a closer look at the process of solving the &#8216;Finding the Direction of a Line Segment&#8217; problem using C++. This problem is geometric in nature and requires a basic understanding of points and vectors. Problem Description &hellip; \ub354 \ubcf4\uae30 &quot;C++ Coding Test Course, Finding the Direction of a Line Segment&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/34214\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:25:36+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T10:58:09+00:00\" \/>\n<meta name=\"author\" content=\"root\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:site\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:label1\" content=\"\uae00\uc4f4\uc774\" \/>\n\t<meta name=\"twitter:data1\" content=\"root\" \/>\n\t<meta name=\"twitter:label2\" content=\"\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04\" \/>\n\t<meta name=\"twitter:data2\" content=\"3\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/34214\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34214\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"C++ Coding Test Course, Finding the Direction of a Line Segment\",\"datePublished\":\"2024-11-01T09:25:36+00:00\",\"dateModified\":\"2024-11-01T10:58:09+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34214\/\"},\"wordCount\":460,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"C++ Coding Test Tutorials\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/34214\/\",\"url\":\"https:\/\/atmokpo.com\/w\/34214\/\",\"name\":\"C++ Coding Test Course, Finding the Direction of a Line Segment - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:25:36+00:00\",\"dateModified\":\"2024-11-01T10:58:09+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34214\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/34214\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/34214\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"C++ Coding Test Course, Finding the Direction of a Line Segment\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/atmokpo.com\/w\/#website\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/atmokpo.com\/w\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"ko-KR\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"contentUrl\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"width\":400,\"height\":400,\"caption\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\"},\"image\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/bebubo4\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\",\"name\":\"root\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"caption\":\"root\"},\"sameAs\":[\"http:\/\/atmokpo.com\/w\"],\"url\":\"https:\/\/atmokpo.com\/w\/author\/root\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"C++ Coding Test Course, Finding the Direction of a Line Segment - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/atmokpo.com\/w\/34214\/","og_locale":"ko_KR","og_type":"article","og_title":"C++ Coding Test Course, Finding the Direction of a Line Segment - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"The coding test includes various problems to assess programming skills in the field. In this article, we will take a closer look at the process of solving the &#8216;Finding the Direction of a Line Segment&#8217; problem using C++. This problem is geometric in nature and requires a basic understanding of points and vectors. Problem Description &hellip; \ub354 \ubcf4\uae30 \"C++ Coding Test Course, Finding the Direction of a Line Segment\"","og_url":"https:\/\/atmokpo.com\/w\/34214\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:25:36+00:00","article_modified_time":"2024-11-01T10:58:09+00:00","author":"root","twitter_card":"summary_large_image","twitter_creator":"@bebubo4","twitter_site":"@bebubo4","twitter_misc":{"\uae00\uc4f4\uc774":"root","\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04":"3\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/34214\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/34214\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"C++ Coding Test Course, Finding the Direction of a Line Segment","datePublished":"2024-11-01T09:25:36+00:00","dateModified":"2024-11-01T10:58:09+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/34214\/"},"wordCount":460,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["C++ Coding Test Tutorials"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/34214\/","url":"https:\/\/atmokpo.com\/w\/34214\/","name":"C++ Coding Test Course, Finding the Direction of a Line Segment - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:25:36+00:00","dateModified":"2024-11-01T10:58:09+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/34214\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/34214\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/34214\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"C++ Coding Test Course, Finding the Direction of a Line Segment"}]},{"@type":"WebSite","@id":"https:\/\/atmokpo.com\/w\/#website","url":"https:\/\/atmokpo.com\/w\/","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","description":"","publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/atmokpo.com\/w\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"ko-KR"},{"@type":"Organization","@id":"https:\/\/atmokpo.com\/w\/#organization","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","url":"https:\/\/atmokpo.com\/w\/","logo":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/","url":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","contentUrl":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","width":400,"height":400,"caption":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8"},"image":{"@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/bebubo4"]},{"@type":"Person","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7","name":"root","image":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","caption":"root"},"sameAs":["http:\/\/atmokpo.com\/w"],"url":"https:\/\/atmokpo.com\/w\/author\/root\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34214","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/comments?post=34214"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34214\/revisions"}],"predecessor-version":[{"id":34215,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34214\/revisions\/34215"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=34214"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=34214"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=34214"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}